2012-11-14 2 views
0

JSON과 관련하여 도움이 필요합니다. 다음과 같이 나는 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" 
       } 
      } 
} 

을 나는 필드 Category에서 모든 데이터를 얻을 싶어요.

나는이 방법으로 시도 :

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

을하지만 난 얻을 오류 :

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

답변

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

당신은 folowing하려면해야
1

.

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

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

당신은 site

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(); 
    } 
에 hierarhy 볼 수 있습니다