解析JSON以更新Android中的listview

时间:2013-05-23 20:09:00

标签: android json android-listview

我想在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]));
                }
            }

logcat的

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":""}]

以下是我从服务器和Log获取的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":""}]

第二次回复有什么问题?

如果需要更多信息,请告诉我。

3 个答案:

答案 0 :(得分:1)

尝试使用此解析器。它将每个数组加载到hashmap - &gt;关键和价值。尽量不要使用硬编码。这可能是坏习惯。

private List<HashMap<String, String>> parseJSonArray(JSONArray array) {
        // Container
        List<HashMap<String, String>> returnArray = new ArrayList<HashMap<String, String>>();
        try {
                for (int i = 0; i < array.length(); i++) {
                        // Download json object
                        JSONObject jsonObject = array.getJSONObject(i);
                        // Prepare temp hashmap
                        HashMap<String, String> temporaryArray = new HashMap<String, String>();
                        // Operate on every keys
                        Iterator iterator = jsonObject.keys();
                        while (iterator.hasNext()) {
                                String key = iterator.next().toString();
                                // Save data to temp container
                                temporaryArray.put(key, jsonObject.get(key).toString());
                        }

                        // Add to main container
                        returnArray.add(temporaryArray);
                }
        } catch (JSONException e) {
                Log.e(TAG, e.getMessage());
        }

        // Return ready List
        return returnArray;
}

然后您可以通过以下方式拨打您的清单:

mylist.get(index of hashmap).get(get value by inputting your string key)

例如

String s = mylist.get(0).get("title");

答案 1 :(得分:1)

第二个JSON响应中的问题是Caloriescarbohydrates为空,而我是空的String,请检查如下。

 if ((calories != null && calories.length() > 0)
                        && (carbohydrates != null && carbohydrates.length() > 0)) {
                    String[] tokens = carbohydrates.split(" ");
                    pair.add(new MyStringPair(title, calories + "/" + tokens[0]));
                }

答案 2 :(得分:0)

将其存储在数组列表中,并在getView方法中使用arraylist的get()方法相应地设置它