Java 奇怪的json空指针
我犯了一个奇怪的错误: 我试图从url解析json对象。这是一个完美的例子,这是json数据:Java 奇怪的json空指针,java,android,json,Java,Android,Json,我犯了一个奇怪的错误: 我试图从url解析json对象。这是一个完美的例子,这是json数据: {"type":"result","rid":"djoezradio", "data":[{ "title":"Webradio", "song":"Test", "track":{ "artist":"Test", "title":"Test", "album":"", "Test":422, "id":423, "playlist":{ "id":14,"title":"regg
{"type":"result","rid":"djoezradio",
"data":[{
"title":"Webradio",
"song":"Test",
"track":{
"artist":"Test",
"title":"Test",
"album":"",
"Test":422,
"id":423,
"playlist":{
"id":14,"title":"reggae"
},
"imageurl":"http:\/\/example.com\/static\/example\/covers\/nocover.png"},
"bitrate":"128 Kbps",
"server":"Online","autodj":"Online","source":"Yes","offline":false","listeners":1,
"maxlisteners":500,"reseller":0,"serverstate":true,"sourcestate":true,
"sourceconn":true,"date":"Dec 14, 2013",
"time":"02:13 PM","url":"http:\/\/example.com\/"}]}
这是我的代码:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://example.com"));
HttpResponse response;
try {
response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
String line = "";
while ((line = in.readLine()) != null) {
JSONObject jObject = new JSONObject(line);
String temp = jObject.getString("imageurl");
Log.e("rid",temp);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
例如,当我执行getString(“playlist”)时,它工作得很好,它将返回id:14等。
唯一不起作用的是对象imageurl。。。
当我想解析它时,它只返回null,而它就在那里
有什么想法吗?
有什么原因吗?因为它是.jpeg?
请分享,我真的卡住了。试试这个
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://example.com"));
HttpResponse response;
try {
response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line;
while((line=in.readLine())!=null)
{
builder.append(line);
}
String JSONdata = builder.toString();
Log.i("JsonData",JSONdata);
JSONObject jObject = new JSONObject(JSONdata);
String temp = jObject.getString("imageurl");
Log.e("rid",temp);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
编辑
JSONObject jObject = new JSONObject(JSONdata);
JSONArray jdata = jObject.getJSONArray("data");
JSONObject job = jdata.getJSONObject(0);
JSONObject jObjt = job.getJSONObject("track");
String temp = jObjt.getString("imageurl");
Log.e("rid",temp);
你确定你正确发布了Json吗?它有不平衡的大括号.thnx,但我仍然可以解析任何对象,除了imageurl。。。。它仍然返回null。Log.e(“JsonData”,JsonData);显示整个json数组,其中还包含imageurl。。。仍然在想为什么它返回null。。虽然不是…:(@iLuvCode您需要从该json中获得哪些数据,并在日志中发布该响应。我需要的唯一数据是imageurl。例如,当我使用log.e(rid,jObject.getString(“playlist”))它只是工作,但当我想对imageurl执行相同操作时,imageurl返回null,而值就像播放列表一样。@iLuvCode发布完整的
JsonData
,因为需要知道它是否有效。它有效,我现在发布了整个JsonData。我检查了它是否有效。我的问题不在那里。