Java 带有日期的Android JSON解析错误
在我的Android应用程序中解析此JSON数据时出现问题:Java 带有日期的Android JSON解析错误,java,android,json,parsing,Java,Android,Json,Parsing,在我的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":"190
[{"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对象中提取生日细节,以及姓名、电子邮件、信用卡和周年纪念
任何建议都将不胜感激 变化
respObject = new JSONObject(EntityUtils.toString(httpEntity));
到
当然respObject必须是一个
JSONArray
,因为您试图解析的对象实际上是一个JSONArray而不是JSONObject。。所以你从JSONArray获得JSONObject,就像
JSONArray JSONArray=新的JSONArray(EntityUtils.toString(httpEntity))
jsonObject jsonObject=jsonArray.getJSONObject(0)
从这个jsonObject可以获得生日详细信息…问题在于JSON字符串。您需要拆下方形制动器[]。 方括号用于表示数组元素。JSON解析器将尝试将其转换为数组对象,因为JSON数据位于square braket中
如果对您有帮助,请接受我的回答。谢谢,我完全忽略了[],因为我们的API的所有其他响应都是简单的JSONObject而不是JSONArray。
respObject = new JSONArray(EntityUtils.toString(httpEntity));