0

URL から JSON を解析したい。私のJSONには次の構造が含まれています:

{
    "HasError": false,
    "ErrorMessage": null,
    "IsFinal": true,
    "content":
     {
        "item":[
                   {
                       "category": "xxx"
                   },
                   {
                       "category": "yy"
                   }
               ]
     }
}

この構造の読み方は?例または JSON パーサーが役立ちます。

4

2 に答える 2

1

Use Gson from Google. The first method is for you to retrieve the data you have received in JSON format, and the other is to convert the object and send as JSON. You can use anytype of object. You can create a class that have all those parameters and send/receive with an instance of that class. You only need to convert from JSON to string, something is easily done with toString()

import com.google.gson.Gson;    


public class JSONParser
{
    private static Gson gson = new Gson();

    public static Data jsonToData(String json)
    {   
        return gson.fromJson(json, Data.class);
    }


    /**
     * Converts the object to JSON
     * @param obj object from type Object
     * @return String of JSON format
     */
    public static String objectToJson(Object obj)
    {
        Gson writer = new Gson();
        return writer.toJson(obj);
    }
}
于 2013-09-18T02:18:53.313 に答える
1

以下でこれを試してください。

String str = "{ \"HasError\": false, \"ErrorMessage\": null, \"IsFinal\": true, \"content\": { \"item\":[ { \"category\": \"xxx\" }, { \"category\": \"yy\" } ] } }";
        try {
             JSONObject jObject=new JSONObject(str);
             Log.d("HasError==", jObject.getString("HasError"));
             Log.d("ErrorMessage==", jObject.getString("ErrorMessage"));
             Log.d("IsFinal==", jObject.getString("IsFinal"));
             jObject = jObject.getJSONObject("content");
             JSONArray jArrayObject = new JSONArray(jObject.getString("item"));
             for (int i = 0; i<jArrayObject.length(); i++) {
                 Log.d("category==", jArrayObject.getJSONObject(i).getString("category").toString());
             }
        } catch (Exception e) {
        }
于 2013-09-18T07:45:00.233 に答える