1

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

第二个反应有什么问题?

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

4

3 回答 3

1

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

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");
于 2013-05-24T09:21:22.433 回答
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]));
                }
于 2013-05-29T22:04:23.240 回答
0

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

于 2013-05-23T20:51:10.803 回答