Android 未解析 JSON 数组
Android not parsing JSON Array
我目前正在尝试在我的代码中解析一个 JSON 数组,但问题是它从未进入 try 块。
这是我的代码:
protected Void doInBackground(Void... arg0) {
JsonParser jsonParser = new JsonParser();
String json = jsonParser.getJSONFromUrl("http://hills.ccsf.edu/~jpark41/ViB/sample.json");
if(json != null) {
Log.i("JSON: ", "NOT NULL"); // This log executes!!
try
{
Log.i("we: ", " out here"); // This does not!!
JSONObject jObj = new JSONObject(json);
JSONArray jsar = new JSONArray(json);
Iterator<String> iter = jObj.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject value = (JSONObject)jObj.get(key);
id = value.getString("id");
thumb = value.getString("thumb");
media = value.getString("media");
title = value.getString("title");
} catch (JSONException e) {
// Something went wrong!
}
now_playing = id;
earned = title;
}
} catch (JSONException e) {
Log.i("we NOT: ", " out here");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
"we out here" 从未被记录,只有 "we NOT out here"。为什么 try 块从不执行?我没有太多用 android 解析 JSON 的经验,所以我确定我只是遗漏了一些东西。感谢您的帮助。
当我注释掉 JSONObject jObj = new JSONObject(json);并将迭代器换成一个基本的 for 循环,一切正常。感谢大家的帮助。
我目前正在尝试在我的代码中解析一个 JSON 数组,但问题是它从未进入 try 块。
这是我的代码:
protected Void doInBackground(Void... arg0) {
JsonParser jsonParser = new JsonParser();
String json = jsonParser.getJSONFromUrl("http://hills.ccsf.edu/~jpark41/ViB/sample.json");
if(json != null) {
Log.i("JSON: ", "NOT NULL"); // This log executes!!
try
{
Log.i("we: ", " out here"); // This does not!!
JSONObject jObj = new JSONObject(json);
JSONArray jsar = new JSONArray(json);
Iterator<String> iter = jObj.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject value = (JSONObject)jObj.get(key);
id = value.getString("id");
thumb = value.getString("thumb");
media = value.getString("media");
title = value.getString("title");
} catch (JSONException e) {
// Something went wrong!
}
now_playing = id;
earned = title;
}
} catch (JSONException e) {
Log.i("we NOT: ", " out here");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
"we out here" 从未被记录,只有 "we NOT out here"。为什么 try 块从不执行?我没有太多用 android 解析 JSON 的经验,所以我确定我只是遗漏了一些东西。感谢您的帮助。
当我注释掉 JSONObject jObj = new JSONObject(json);并将迭代器换成一个基本的 for 循环,一切正常。感谢大家的帮助。