我想在 textview 中动态打印 json 数组 utrition 数组,问题是如果我在这一行中写“1” (school3.getJSONObject("1")); 然后它只打印来自json的第一个“名称”和“数量”。我想在这一行写 j 但问题是它显示错误 JSONObject 类型中的方法 getJSONObject(String) 不适用于参数 (int)
for (int j = 1
school3.getJSONObject(j)
有没有任何方法转换 int j= string 然后像这样添加 school3.getJSONObject(j)
//for example
string z;
z=j.tostring();
school3.getJSONObject(z);
{
"status":1,
"data"
,
"dish_nutrition":
{
"1":
{
"name":"Cholesterol and Diet",
"qty":"2"
},
"2":
{
"name":"Cholesterol and Diet",
"qty":"1"
}
}
}
JSONObject school3 = json2.getJSONObject("dish_nutrition");
final TableLayout table = (TableLayout) findViewById(R.id.table2);
for (int j = 1; j < school3.length(); j++) {
final View row = createRow (school3.getJSONObject("1"));
table.addView(row);
num=num+1;
}
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;
}