1

Android で JSON を解析したいのですが、1 つの応答を解析できますが、もう 1 つの応答は解析できません。

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

どちらの場合もサーバーからデータを取得していますが、リストビューで応答 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":""}]

以下は、サーバーとログから取得している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":""}]

2 番目の応答の何が問題になっていますか?

さらに情報が必要な場合はお知らせください。

4

3 に答える 3

1

このパーサーを使用してみてください。すべての配列をハッシュマップにロードします->キーと値。ハードコーディングを使用しないようにしてください。それは悪い習慣かもしれません。

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 に答える
0

それを配列リストに格納し、getView メソッドで、arraylist の get() メソッドを使用してそれに応じて設定します。

于 2013-05-23T20:51:10.803 に答える