0

について助けが必要JSONです。次のように取得JSONします。

{
    "1": {
    "id": "1",
    "position": "1",
    "Category": {
        "id": "1",
        "position": "1",
        "created_at": "2012-10-24 15:42:47",
        "updated_at": "2012-11-13 13:46:25",
        "name": "ABCD"
                }
          }
 }

field からすべてのデータを取得したいCategory

私はこの方法を試します:

JSONObject categoryObject = json.getJSONObject("Category"); 

しかし、私はエラーが発生します:

no value for Category. How I can get data for Category field?

4

3 に答える 3

1
JSONObject categoryObject = json.getJSONObject("1").getJSONObject("Category");  
categoryObject.get("id"); // return 1  
categoryObject.get("position"); // return 1   
categoryObject.get("name"); // return "ABCD"

于 2012-11-14T08:00:39.497 に答える
1

次のことを行う必要があります。

JSONObject json1 = json.getJSONObject("1");

JSONObject categoryObject = json1.getJSONObject("カテゴリ");

サイト上でヒエラルキーを見ることができます

于 2012-11-14T08:00:40.217 に答える
0
try {
        String jsonString="{\"1\":{\"id\":\"1\",\"position\":\"1\",\"Category\":{\"id\":\"1\",\"position\":\"1\",\"created_at\":\"2012-10-24 15:42:47\",\"updated_at\":\"2012-11-13 13:46:25\",\"name\":\"ABCD\"}}}";
        JSONObject jsonObject=new JSONObject(jsonString);
        JSONObject jsonObject1= jsonObject.getJSONObject("1");

        String id=jsonObject1.getString("id");
        String position =jsonObject1.getString("position");

        JSONObject jsonObjectCategory= jsonObject1.getJSONObject("Category");

        String idCategory=jsonObjectCategory.getString("id");
        String positionCategory=jsonObjectCategory.getString("position");
        String createdAt=jsonObjectCategory.getString("created_at");
        String updatedAt=jsonObjectCategory.getString("updated_at");
        String name=jsonObjectCategory.getString("name");

        Log.e(getClass().getSimpleName(), "name="+name +"; created_at= "+createdAt);
    } catch (JSONException e) {
        e.printStackTrace();
    }
于 2012-11-14T08:10:53.383 に答える