如何访问多数组json文件?

时间:2013-08-05 09:03:04

标签: android

这是我下面的代码如何访问这个json文件检查我的方法它是aces json文件数组和节点的coreect方法?我想从我的代码访问下面这个json文件帮助我如何访问这个json文件formmy代码?我不知道json文件中的howmany数组

{
    "status":1,
    "message":"",
    "data":
    {
        "school":
        [
        {
            "id":3,
            "name":"FG Public School"
        },
        {
            "id":4,
            "name":"Fazaia Inter College"}
        ]
    }
}




HttpClient client = new DefaultHttpClient();

HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);

HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(SelectMenuAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = 
response.getEntity().getContent();
BufferedReader in = new BufferedReader(new 
InputStreamReader(atomInputStream));

String line;
String str = "";
while ((line = in.readLine()) != null){
    str += line;
}


JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("school");

for (int i = 0; i < data.length(); i++) {
    JSONObject object =  
    data.getJSONObject(i); 

    //    JSONObject category = 
    object.getJSONObject("Category");


    Category_ID.add(Long.parseLong(object.getString("id")));

    Category_name.add(object.getString("name"));

    Log.d("Category name", 
    Category_name.get(i));

3 个答案:

答案 0 :(得分:1)

Try This Code
=================================================================
    try {
            String[] Id,name;
          JSONObject json = new JSONObject(str);

          JSONObject SubString3 = json.getJSONObject("data");
          Log.e(SubString3.toString(),"SubString3");
          JSONArray Array = SubString3.getJSONArray("school");
          Id = new String[Array.length()];
          name =new String[Array.length()]; 
          for(int i=0;i<=Array.length();i++)
          {
              Id[i]= Array.getJSONObject(i).getString("id");
              Log.e(Id[i].toString(),"Id[i]");
              name[i]= Array.getJSONObject(i).getString("name");
              Log.e(name[i].toString(),"name[i]");
          }
      } catch (JSONException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }

答案 1 :(得分:1)

Json文件中没有Category对象

你可以试试

    JSONObject json = new JSONObject("str");
    JSONObject data = json.getJSONObject("data");
    JSONArray school = json.getJSONArray("school");

    for (int i = 0; i < data.length(); i++) {
        JSONObject object = school.getJSONObject(i);

        long id = Long.parseLong(object.getString("id"));
        String name = object.getString("name");
        JSONObject category = new JSONObject();
                    category.put("id");
                    category.put("name");




    }

答案 2 :(得分:0)

首先你需要获取整个JSON的OBJECT,因为它在'{''}'中。然后,您需要再次使用名称“data”获取对象,因为它位于“{''}'中。只有这样你才能获取名为“school”的数组。

JSONObject json = new JSONObject(str);
JSONObject data = json.getJSONObject("data");
JSONArray school = data.getJSONArray("school");