簡體   English   中英

加載JSON-Array失敗

[英]Loading a JSON-Array fails

我通過HTTP-Response獲得的JSON-String / Array在一行中,如下所示:

[{
        "haendlerName": "Zielpunkt",
        "shops": [{
            "shopId": 243779,
            "ort": "Wien",
            "strasse": "Erdbergstraße 61",
            "plz": "1030",
            "lat": 48.19867,
            "lon": 16.400263,
            "distance": 0.14937061106081023,
            "openinghours": null
        }],
        "imageLink": "http://images.schnapp.at/images/zielpunkt__e349e2a937b5bf4f78e0fb3063b1fca8.png",
        "account_id": 171619
    }, ...

我正在像這樣加載它:

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpclient.execute(httpGet);
HttpEntity resEntity = response.getEntity(); 
String response2=EntityUtils.toString(resEntity);
JSONArray finalResult = new JSONArray(response2);

編輯:工作版本!

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);    
response = httpclient.execute(httpGet);
String json = EntityUtils.toString(response.getEntity());
finalResult = new JSONArray(json);

但是在最后一行它崩潰了,我得到的唯一錯誤是NullPointerException。 我檢查了令牌程序的內容,似乎還可以。 還檢查了整個輸入(http響應中的字符串),它是有效的JSON字符串。

可能值得一提的是,主數組中的JSONObjects也包含一個子數組。

任何想法可能導致崩潰的原因嗎? 還是我做錯了什么?

刪除BufferedReaderString jsonJSONTokener tokener 嘗試這種方式

String json = EntityUtils.toString(response.getEntity());
JSONArray finalResult = new JSONArray(json);

EntityUtils.toString讀取Entity的內容並將其作為String返回

您應該更換:

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
    String json = reader.readLine();
    JSONTokener tokener = new JSONTokener(json);
    JSONArray finalResult = new JSONArray(tokener);

String json = EntityUtils.toString(response.getEntity());
JSONArray finalResult = new JSONArray(json);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM