简体   繁体   English

Android JSON解析日期错误

[英]Android JSON parse error with date

Having problems parsing this JSON data in my Android App : 在我的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}]

The error I am getting is: 我得到的错误是:

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

My JSON Parser code is: 我的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;
}

All I need is to pull out the Birthday details from this JSON object along with the name, email, credits and anniversary. 我需要做的就是从此JSON对象中提取Birthday的详细信息,以及名称,电子邮件,信用和周年纪念日。

Any advice would be appreciated! 任何意见,将不胜感激!

Change 更改

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

to

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

ofcourse respObject has to be an JSONArray 当然respObject必须是JSONArray

The problem is with your JSON String. 问题出在您的JSON字符串上。 You need to remove the square brakets [] . 您需要卸下方形刹车片[] Square brakets are used to refer array elements. 方格用于引用数组元素。 The JSON parser will try to convert it as array object as json data are inside square braket. JSON解析器将尝试将其转换为数组对象,因为JSON数据位于方形框内。

Accept my answer if it helps you. 如果可以,请接受我的回答。

Since the object you are trying to parse is actually a JSONArray and not JSONObject.. So you get JSONObject from that JSONArray like 由于您要解析的对象实际上是JSONArray而不是JSONObject。因此您可以从该JSONArray中获取JSONObject,例如

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

JSONOject jsonObject = jsonArray.getJSONObject(0); JSONOject jsonObject = jsonArray.getJSONObject(0);

from this jsonObject you would get birthday details... 从这个jsonObject你会得到生日的详细信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM