0

这是我下面的 json 代码工作正常,但唯一的问题是一次显示一个 for 循环数据,如果我评论
JSONArray school2 = school.getJSONArray("contains");循环,那么 JSONArray data2 = school.getJSONArray("nutritions");只会显示其他明智的

JSONArray school2 = school.getJSONArray("contains");数据不会同时显示两个 for 循环数据将同时显示

        JSONObject json = new JSONObject(str);

        JSONObject json2 = new JSONObject(str);
        message = json2.getString("message");
        status = json2.getString("status");
        if (status.equals("1")) {

            JSONObject data = json.getJSONObject("data");

            JSONObject school = data.getJSONObject("entree");

            txt1.setText(school.getString("name"));
            // txt2.setText(school.getString("name"));
            txt3.setText(school.getString("description"));

            String url1 = school.getString("image");

            imgLoader.DisplayImage(url1, img1);

            JSONArray school2 = school.getJSONArray("contains");
            //
            for (int i = 0; i < school.length(); i++) {
                JSONObject object = school2.getJSONObject(i);


                    Category_ID.add(Long.parseLong(object.getString("id")));
                Category_name.add(object.getString("name"));
                Category_image.add(object.getString("image"));
                listview.setAdapter(cla);

            }

            JSONArray data2 = school.getJSONArray("nutritions");
            // JSONArray data2 = json.getJSONArray("contacts");
            final TableLayout table = (TableLayout) 
           findViewById(R.id.table2);
            for (int j = 0; j < data2.length(); j++) {
                final View row = createRow(data2.getJSONObject(j));
                table.addView(row);

            }

        } else {

            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        // IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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("weight"));

    return row;
}
           }



         if i comment this code then  JSONArray data2 =  
     school.getJSONArray("nutritions"); will show



    //              JSONArray school2 = 
 school.getJSONArray("contains");

 //             //
 //             for (int i = 0; i < school.length(); i++) {
 //                 JSONObject object = school2.getJSONObject(i);
 //
 //                   
   Category_ID.add(Long.parseLong(object.getString("id")));
 //                 Category_name.add(object.getString("name"));
 //                 Category_image.add(object.getString("image"));
 //                 listview.setAdapter(cla);
 //
 //             }
4

0 回答 0