[英]how to get json array and object
以下是我的代碼,我想在代碼bur中解析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>
您正在通過以下方法將int傳遞給方法getJSONObject()
:createRow(school3.getJSONObject(j)。j是一個int。
school3是一個對象而不是數組。 您可以通過school3.getJSONObject("1")
獲得值。 如果可能的話,也許您想使用數組而不是對象? 現在,您可以通過執行以下school3.getJSONObject("1").getString("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");
}
我建議您花一些時間來學習Jackson的高性能JSON處理器Java庫。 使用非常簡單,但功能卻非常強大
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.