1

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

{
    "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));
4

3 回答 3

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();
      }
于 2013-08-05T09:21:59.023 回答
1

类别对象不存在于 Json 文件中

您可以尝试以下

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




    }
于 2013-08-05T09:49:40.103 回答
0

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

JSONObject json = new JSONObject(str);
JSONObject data = json.getJSONObject("data");
JSONArray school = data.getJSONArray("school");
于 2013-08-05T09:17:53.773 回答