簡體   English   中英

Android JSON解析日期錯誤

[英]Android JSON parse error with date

在我的Android應用程序中解析此JSON數據時遇到問題:

[{"personid":20,"personName":"Ross Gallagher update3","email":"ross_gallagher@rossgallagher.co.uk","birthday":{"date":"2013-01-01 00:00:00","timezone_type":3,"timezone":"America\/Los_Angeles"},"anniversary":{"date":"1900-01-01 00:00:00","timezone_type":3,"timezone":"America\/Los_Angeles"},"Credit":2}]

我得到的錯誤是:

W/System.err: org.json.JSONException: Value [{"birthday":{"date":"2013-01-01 00:00:00","timezone":"America\/Los_Angeles","timezone_type":3},"anniversary":{"date":"1900-01-01 00:00:00","timezone":"America\/Los_Angeles","timezone_type":3},"email":"ross_gallagher@rossgallagher.co.uk","personName":"Ross Gallagher update8","Credit":2,"personid":20}] of type org.json.JSONArray cannot be converted to JSONObject

我的JSON解析器代碼是:

public JSONObject getJSONFromUrl(String url)
{
    HttpEntity httpEntity = null;
    JSONObject respObject = null;

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if(httpEntity != null){
        try {
            respObject = new JSONObject(EntityUtils.toString(httpEntity));
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //....
    }
    // return JSON
    return respObject;
}

我需要做的就是從此JSON對象中提取Birthday的詳細信息,以及名稱,電子郵件,信用和周年紀念日。

任何意見,將不勝感激!

更改

respObject = new JSONObject(EntityUtils.toString(httpEntity));

respObject = new JSONArray(EntityUtils.toString(httpEntity));

當然respObject必須是JSONArray

問題出在您的JSON字符串上。 您需要卸下方形剎車片[] 方格用於引用數組元素。 JSON解析器將嘗試將其轉換為數組對象,因為JSON數據位於方形框內。

如果可以,請接受我的回答。

由於您要解析的對象實際上是JSONArray而不是JSONObject。因此您可以從該JSONArray中獲取JSONObject,例如

JSONArray jsonArray =新的JSONArray(EntityUtils.toString(httpEntity));

JSONOject jsonObject = jsonArray.getJSONObject(0);

從這個jsonObject你會得到生日的詳細信息...

暫無
暫無

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

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