2016-05-29 109 views
0

我想从JSON数组中获取JSON字符串,但得到这个错误,任何帮助将高度赞赏,JSON合成器给出了波纹管,请帮助别人。 错误是波纹管值java.lang.String类型的HTTPS无法转换为JSONArray

05-29 12:37:22.600 25505-25505/com.akasha.mongodataapi W/System.err: org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONArray 
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err:  at org.json.JSONArray.<init>(JSONArray.java:96) 
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err:  at org.json.JSONArray.<init>(JSONArray.java:108) 

它是JSON的甲酸

[ { "_id" : { "$oid" : "57472009a0fdab7cc3c"} , "name" : "Sasha Burni" , "sort" : "Sasha"} , 
{ "_id" : { "$oid" : "57472009afdab7cc3d"} , "name" : "Akasha Khail" , "sort" : "Akasha"}] 

和Java代码是波纹管

String url="https://api.mlab.com/api/1/databases/picasanovels/collections/Country?apiKey=myapikey"; 

    try { 
       JSONArray jArr = new JSONArray(url); 
       for (int count = 0; count < jArr.length(); count++) { 
        JSONObject obj = jArr.getJSONObject(count); 
        String name = obj.getString("name"); 
        System.out.println("Name Printed :"+name); 
        //so on 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
+0

它看起来像它试图您的网址转换成JSON阵列。你必须进行GET请求才能获取该URL的内容,然后将该内容作为参数传递给JSONArray对象 – bwalshy

+0

您是不是将url转换为String类型为json数组?我想你想要转换http请求的响应,而不是你所做的。 使用一些http客户端,如Unirest或apache http客户端发出请求。 – user1211

+0

感谢您的早期回复,但是如果有一些例子会很容易理解。 –

回答

0
  1. 您正在尝试通过API调用(URL地址)到JSON ARRAY。它应该是来自API调用的响应。
  2. 您必须创建一个URL对象并将Url传递给构造函数。
  3. 然后,你必须使用HttpUrlConnection类来打开一个新的连接。

我已经在下面列出了完整的代码供您参考。请随时询问您是否需要更多帮助。由于

package com.pragin.ocjp; 

import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class JsonExample { 

public String jsonEx(){ 
    String url = " https://api.tfl.gov.uk/Place?lat=51.555504&lon=0.0592359&radius=80&includeChildren=False&type=NaptanPublicBusCoachTram&app_id=95a7b158&app_key=a3acd48055f470cf35ab5f6f360604c5"; 
    URL obj; 
    BufferedReader reader; 
    StringBuilder stringBuilder; 
    HttpURLConnection con; 
    stringBuilder = new StringBuilder(); 
    try { 
     obj = new URL(url); 
     con = (HttpURLConnection) obj.openConnection(); 
     con.setRequestMethod("GET"); 
     int responseCode = con.getResponseCode(); // to check success and failure of API call 

     System.out.println("Response Code : " + responseCode); 
     String response = con.getResponseMessage(); 

     System.out.println("Response : " + response.toString()); 

     reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 


     String line = null; 

     while ((line = reader.readLine()) != null){ 
      stringBuilder.append(line + "\n"); 

      System.out.println("String : " + stringBuilder.toString()); 
     } 
     //return stringBuilder.toString(); 

    }catch(IOException e){ 
     System.out.println("Error" + e); 
    } 
    return stringBuilder.toString(); 

} 

public void jsonParser(String res) throws ParseException { 
    JSONParser jParse = new JSONParser(); 

    JSONObject jsonObject = (JSONObject) jParse.parse(res); 
    System.out.println("res = [" + jsonObject.size() + "]"); 
    for(int i = 0; i <jsonObject.size(); i++){ 
     // JSONObject jsonObject = (JSONObject) jArray.get(i); 
     // jsonObject. 
     String name = (String) jsonObject.get("$type"); 
     System.out.println("Name : " + name); 
    } 

} 

public static void main(String[] args) throws ParseException { 
    JsonExample js = new JsonExample(); 
    js.jsonParser(js.jsonEx()); 
} 

}`

相关问题