2013-08-28 27 views
0

是我的代碼,我想在我的代碼鑽IM沒有得到陣列「dish_nutrition」解析JSON數據:在類型線路6的方法getJSONObject(串)的示值誤差的JSONObject不適用於參數(int)請告訴我我該怎麼做? 什麼是解析這些數據的正確方法?如何獲得JSON數組和對象下面

"status":1, 
"data": 
"dish_nutrition": 
{"1": 
{"name":"Cholesterol and Diet", 
"qty":"2"}, 
"2": 
{"name":"Cholesterol and Diet", 
"qty":"1" 
} 
}} 



      JSONObject json2 = new JSONObject(str); 

     status = json2.getString("status"); 
     if (status.equals("1")) { 

        JSONObject school3 = json2.getJSONObject("dish_nutrition"); 




      final TableLayout table = (TableLayout) findViewById(R.id.table2); 
      for (int j = 0; j < school3.length(); j++) { 

    //line6 show rror The method getJSONObject(String) in the type JSONObject is not 
      applicable for the arguments (int)  
    line 6// final View row = createRow(school3.getJSONObject(j)); 
       table.addView(row); 

      } 







      public View createRow(JSONObject item) throws JSONException { 
    View row = getLayoutInflater().inflate(R.layout.rows, null); 
    ((TextView) row.findViewById(R.id.localTime)).setText(item 
      .getString("name")); 
    ((TextView) row.findViewById(R.id.apprentTemp)).setText(item 

    .getString("qty")); 

    return row; 






        <TableLayout 
     android:id="@+id/table2" 
     android:layout_width="fill_parent" 
      android:layout_below="@+id/test_button_text23" 
     android:layout_marginLeft="45dp" 
     android:layout_marginBottom="25dp" 
     android:paddingBottom="20dp" 

     android:layout_marginRight="45dp" 

     android:layout_height="fill_parent" 
     android:stretchColumns="*" > 

    <TableRow 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/border7" 
    > 



    <TextView 
     android:gravity="left" 
     android:textStyle="bold" 
     android:background="@drawable/border7" 
     android:paddingLeft="10dp" 
     android:text="Quantity" /> 
    <TextView 
     android:gravity="center" 
     android:text="Item" 
     android:background="@drawable/border7" 
     android:textStyle="bold" /> 

</TableRow> 

</TableLayout> 



    ///row.xml/////////// 



     <?xml version="1.0" encoding="utf-8"?> 
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 


    <TextView 

    android:id="@+id/apprentTemp" 
    android:textColor="#000000" 
     android:background="@drawable/border7" 
     android:paddingLeft="10dp" 
     android:gravity="left" 


    /> 
<TextView 

    android:id="@+id/localTime" 
    android:textColor="#000000" 
     android:background="@drawable/border7" 
     android:gravity="center" 


    /> 



    </TableRow> 
+0

請告訴我,很簡單「dish_nutrition」:是數組或對象??? –

+0

我想顯示像這樣http://imgur.com/mQd3cmA我更新數據我的上述 –

回答

0

你傳入一個int該方法getJSONObject()在:createRow(school3.getJSONObject(j)的j是一個int

school3是一個對象不是數組可以通過school3.getJSONObject("1")獲取值。 ?也許你想使用的,而不是一個對象數組,如果這是可能的現在,你可以通過做得到name值:school3.getJSONObject("1").getString("name");

使用數組它會是什麼樣子:

"dish_nutrition": 
[ {"name":"Cholesterol and Diet", "qty":"2"}, 
    {"name":"Cholesterol and Diet", "qty":"1"} ] 

然後,您可以獲取數組:json2.getJSONArray("dish_nutrition"); 和環比它:

for (int j = 0; j < school3.length(); j++) { 
    JSONObject jObject = school3.getJSONObject(j); 
    String name = jObject.getString("name"); 
} 
+0

STR所有XML代碼是正確的I調試在該行 的JSONObject school3 = json2.getJSONObject(「dish_nutrition」); school3在調試模式下觀看時顯示所有數據 –

+0

好的。這是school3.getJSONObject。我更新了我的答案。你想要到這裏來? – Klaasvaak

+0

所有JSON文件數據顯示在此階段 的JSONObject school3 = json2.getJSONObject( 「dish_nutrition」); 如何獲得其內部的數據是阿雷或對象? –

0

我建議你花一些時間學習傑克遜高性能JSON處理器Java庫。它是使用但非常強大的