2016-05-29 69 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()); 
} 

}`

相關問題