0

以下は私のコードです。私のコードで json データを解析したいのですが、配列 "dish_nutrition" を取得できません: 6 行目にエラーが表示されます JSONObject 型のメソッド getJSONObject(String) は、引数 (int) には適用できません 助けてください私は何をしますか??このデータを解析する正しい方法は何ですか???

"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>
4

2 に答える 2

0

getJSONObject()createRow(school3.getJSONObject(j)のメソッドに int を渡しています。j は int です。

school3 は配列ではなくオブジェクトです。で値を取得できますschool3.getJSONObject("1")。可能であれば、オブジェクトの代わりに配列を使用したいですか? これで、次のようにして名前の値を取得できます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");
}
于 2013-08-28T19:56:23.913 に答える
0

高性能 JSON プロセッサ Java ライブラリである Jackson の学習に時間を費やすことをお勧めします。使い方は非常に簡単ですが、非常に強力です

于 2013-08-28T20:02:03.927 に答える