我想在 android 中解析 JSON 我可以解析一个响应,而我无法解析另一个响应。
1) 我能够解析以下 Web 服务响应
[
{
"id": "763",
"title": ".46 Magnum recipe",
"Calories": "189",
"Carbohydrates": "5.5 g"
},
{
"id": "332",
"title": "100 Miles per Hour recipe",
"Calories": "317",
"Carbohydrates": "24.8 "
},
{
"id": "6759",
"title": "12 Gauge Shottie recipe",
"Calories": "515",
"Carbohydrates": "27.5 "
},
{
"id": "6760",
"title": "136 recipe",
"Calories": "153",
"Carbohydrates": "22.7 "
},
{
"id": "8986",
"title": "151 Reasons recipe",
"Calories": "113",
"Carbohydrates": "14.9 "
},
{
"id": "6761",
"title": "18 Till You Die recipe",
"Calories": "81",
"Carbohydrates": "3.9 g"
},
{
"id": "333",
"title": "187 Cocktail recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "9031",
"title": "1968 recipe",
"Calories": "66",
"Carbohydrates": "12.3 "
},
{
"id": "6762",
"title": "20/20 Lemon recipe",
"Calories": "258",
"Carbohydrates": "4.8 g"
},
{
"id": "334",
"title": "2000 Flushes recipe",
"Calories": "112",
"Carbohydrates": "6.7 g"
}
]
2)这个响应可以被解析
[
{
"id": "2",
"title": "A Little Dinghy recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "4",
"title": "A Clockwork Tangerine recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "5",
"title": "Tiki Rum recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "6",
"title": "Absolute Monster recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "18",
"title": "Anchors Away recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "21",
"title": "Apple Jell-o Shots recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "24",
"title": "Applegasm recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "27",
"title": "Attempted Suicide recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "32",
"title": "Banana Grape Smoothie recipe",
"Calories": "",
"Carbohydrates": ""
},
{
"id": "39",
"title": "Beautiful Side Ride recipe",
"Calories": "",
"Carbohydrates": ""
}
]
在这两种情况下,我都从服务器获取数据,但无法在 listview 中解析和更新响应 2
下面是我如何解析两个响应的代码
JSONArray jObject = new JSONArray(resultData);
for (int i = 0; i < jObject.length(); i++) {
JSONObject menuObject = jObject.getJSONObject(i);
String title = menuObject.getString("title");
String calories = menuObject.getString("Calories");
String carbohydrates = menuObject.getString("Carbohydrates");
if ((calories != null && calories.length() > 0)
&& (carbohydrates != null && carbohydrates.length() > 0)) {
String[] tokens = carbohydrates.split(" ");
pair.add(new MyStringPair(title, calories + "/" + tokens[0]));
}
}
日志猫
05-24 10:27:53.452: W/System.err(887): org.json.JSONException: End of input at character 0 of
05-24 10:27:53.552: W/System.err(887): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
05-24 10:27:53.562: W/System.err(887): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
05-24 10:27:53.562: W/System.err(887): at org.json.JSONArray.<init>(JSONArray.java:87)
05-24 10:27:53.562: W/System.err(887): at org.json.JSONArray.<init>(JSONArray.java:103)
05-24 10:27:53.562: W/System.err(887): at com.example.mixdrinksv1.MyStringPair.makeData(MyStringPair.java:49)
05-24 10:27:53.562: W/System.err(887): at com.example.mixdrinksv1.SortbyCalTab.onCreateView(SortbyCalTab.java:40)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-24 10:27:53.562: W/System.err(887): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-24 10:27:53.572: W/System.err(887): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-24 10:27:53.582: W/System.err(887): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-24 10:27:53.582: W/System.err(887): at android.os.Handler.handleCallback(Handler.java:605)
05-24 10:27:53.582: W/System.err(887): at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 10:27:53.582: W/System.err(887): at android.os.Looper.loop(Looper.java:137)
05-24 10:27:53.582: W/System.err(887): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 10:27:53.582: W/System.err(887): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 10:27:53.592: W/System.err(887): at java.lang.reflect.Method.invoke(Method.java:511)
05-24 10:27:53.602: W/System.err(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 10:27:53.612: W/System.err(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 10:27:53.612: W/System.err(887): at dalvik.system.NativeStart.main(Native Method)
05-24 10:27:53.741: D/dalvikvm(887): GC_CONCURRENT freed 314K, 5% free 9634K/10055K, paused 13ms+9ms
05-24 10:27:54.402: I/JSONResult(887): [{"id":"2","title":"A Little Dinghy recipe","Calories":"","Carbohydrates":""},{"id":"4","title":"A Clockwork Tangerine recipe","Calories":"","Carbohydrates":""},{"id":"5","title":"Tiki Rum recipe","Calories":"","Carbohydrates":""},{"id":"6","title":"Absolute Monster recipe","Calories":"","Carbohydrates":""},{"id":"18","title":"Anchors Away recipe","Calories":"","Carbohydrates":""},{"id":"21","title":"Apple Jell-o Shots recipe","Calories":"","Carbohydrates":""},{"id":"24","title":"Applegasm recipe","Calories":"","Carbohydrates":""},{"id":"27","title":"Attempted Suicide recipe","Calories":"","Carbohydrates":""},{"id":"32","title":"Banana Grape Smoothie recipe","Calories":"","Carbohydrates":""},{"id":"39","title":"Beautiful Side Ride recipe","Calories":"","Carbohydrates":""}]
下面是我从服务器和日志获取的 json 数据。
24 10:27:54.402: I/JSONResult(887): [{"id":"2","title":"A Little Dinghy recipe","Calories":"","Carbohydrates":""},{"id":"4","title":"A Clockwork Tangerine recipe","Calories":"","Carbohydrates":""},{"id":"5","title":"Tiki Rum recipe","Calories":"","Carbohydrates":""},{"id":"6","title":"Absolute Monster recipe","Calories":"","Carbohydrates":""},{"id":"18","title":"Anchors Away recipe","Calories":"","Carbohydrates":""},{"id":"21","title":"Apple Jell-o Shots recipe","Calories":"","Carbohydrates":""},{"id":"24","title":"Applegasm recipe","Calories":"","Carbohydrates":""},{"id":"27","title":"Attempted Suicide recipe","Calories":"","Carbohydrates":""},{"id":"32","title":"Banana Grape Smoothie recipe","Calories":"","Carbohydrates":""},{"id":"39","title":"Beautiful Side Ride recipe","Calories":"","Carbohydrates":""}]
第二个反应有什么问题?
如果需要更多信息,请告诉我。