Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获取json数组和对象_Android - Fatal编程技术网

Android 如何获取json数组和对象

Android 如何获取json数组和对象,android,Android,下面是我的代码,我想在我的代码中解析json数据,但我没有得到数组“dish_nutrition”:它在第6行显示错误类型JSONObject中的方法getJSONObject(String)不适用于参数(int)请告诉我我该怎么办?? 解析此数据的正确方法是什么 "status":1, "data": "dish_nutrition": {"1": {"name":"Cholesterol and Diet", "qty":"2"}, "2": {"name":"Cholesterol and

下面是我的代码,我想在我的代码中解析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>
“状态”:1,
“数据”:
“菜肴营养”:
{"1":
{“名称”:“胆固醇与饮食”,
“数量”:“2”},
"2":
{“名称”:“胆固醇与饮食”,
“数量”:“1”
}
}}
JSONObject json2=新的JSONObject(str);
status=json2.getString(“status”);
如果(状态等于(“1”)){
JSONObject school3=json2.getJSONObject(“菜肴营养”);
最终的TableLayout表格=(TableLayout)findViewById(R.id.table2);
对于(int j=0;j<3.length();j++){
//第6行显示错误类型JSONObject中的getJSONObject(String)方法不正确
适用于参数(int)
第6行//最终视图行=createRow(school3.getJSONObject(j));
表.addView(行);
}
公共视图createRow(JSONObject项)抛出JSONException{
视图行=GetLayoutFlater()。充气(R.layout.rows,null);
((TextView)row.findviewbyd(R.id.localTime)).setText(项
.getString(“名称”);
((TextView)行findViewById(R.id.apprentTemp)).setText(项目
.getString(“数量”);
返回行;
///row.xml///////////

您正在将一个int传递给方法
getJSONObject()
at:createRow(school3.getJSONObject(j)。j是一个int

school3是一个对象而不是数组。您可以通过
school3.getJSONObject(“1”)
获取值。如果可能,您可能希望使用数组而不是对象?现在您可以通过执行以下操作获取名称值:
school3.getJSONObject(“1”).getString(“名称”)

使用一个数组,它看起来像:

"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");
}
for(int j=0;j
我建议您花些时间学习Jackson的高性能JSON处理器Java库。它使用非常简单,但功能非常强大

请告诉我“dish\u nutrition”:是数组还是对象;school3在调试模式下查看时显示所有数据OK。这是school3.getJSONObject。我更新了我的答案。你想在这里得到什么?所有json文件数据在此阶段显示JSONObject school3=json2.getJSONObject(“dish_”);如何获取其内部数据它是aray或object??那么我该怎么做?你的建议是什么?你是否控制JSON?如果是,请使用数组作为dish_