Android JSON getJSONObject无法正常工作,解析不正确

时间:2014-10-15 07:26:13

标签: android json parsing

我无法弄清楚为什么我的解析不起作用,这是我的JSON:

  {  
   "fileVersion":"1.0",
   "graves":[  
      {  
         "ID_grave":"1",
         "ID_line":"1",
         "sequence":"1",
         "persons":[  
            {  
               "ID_person":"1",
               "name":"Janez",
               "surname":"Novak",
               "dateBirth":"1956-08-11",
               "dateDeath":"2014-02-12",
               "important":"0",
               "imp_desc":""
            }
         ]
      },
      {  
         "ID_grave":"2",
         "ID_line":"1",
         "sequence":"2",
         "persons":[  
            {  
               "ID_person":"2",
               "name":"Mojca",
               "surname":"Novak",
               "dateBirth":"1953-02-13",
               "dateDeath":"2012-04-08",
               "important":"0",
               "imp_desc":""
            }
         ]
      }
   ]
}

当我想获得第一个JSONObject:

时,此代码正常工作
 String jsonData = convertStreamToString(in);

         JSONObject json = new JSONObject(jsonData);  
         JSONArray name = json.getJSONArray("graves");

for (int i = 0; i < name.length(); i++) {
         JSONObject grave = name.getJSONObject(i);   
         lineArrayList.add(grave.getString("ID_line"));
         graveArrayList.add(grave.getString("ID_grave"));
}

但是我想得到#34;&#34;数组在&#34; graves&#34;宾语。这应该有效,但事实并非如此,我只得到第一个人数组,其名称是Janez,而不是名为Mojca的第二个数组:

    String jsonData = convertStreamToString(in);

         JSONObject json = new JSONObject(jsonData);  
         JSONArray name = json.getJSONArray("graves");

    for (int i = 0; i < name.length(); i++) {
         JSONObject grave = name.getJSONObject(i);  
         JSONArray persons = grave.getJSONArray("persons");

         for (int k = 0; k < persons.length(); k++) {   

//The problem was because of the index i, you have to change to k and it will work
          JSONObject grave = persons.getJSONObject(i);
           nameArrayList.add(grave.getString("name"));
          surnameArrayList.add(grave.getString("surname"));

         }

      }

4 个答案:

答案 0 :(得分:1)

坟墓是JSONArray而人是JSONArraygraves

 for (int i = 0; i < name.length(); i++) {
      JSONObject grave = name.getJSONObject(i);   
      JSONArray persons = grave.optJSONArray("persons");
      if (persons != null) {
         for (int j = 0; j < persons.length(); j++) {

         }
      }
 }

答案 1 :(得分:1)

按照以下方式进行解析,

String jsonData = convertStreamToString(in);

     JSONObject json = new JSONObject(jsonData);  
     JSONArray name = json.getJSONArray("graves");

for (int i = 0; i < name.length(); i++) {
     JSONObject grave = name.getJSONObject(i);  
     JSONArray persons = grave.getJSONArray("persons");

     for (int k = 0; k < persons.length(); k++) {

      JSONObject grave = persons.getJSONObject(i);
       nameArrayList.add(grave.getString("name"));
      surnameArrayList.add(grave.getString("surname"));

     }

  }

答案 2 :(得分:0)

试试这个:

  for (int i = 0; i < name.length(); i++) {
          JSONObject grave = name.getJSONObject(i);   
          JSONArray persons = grave.optJSONArray("persons");
          if (persons != null) {
             for (int j = 0; j < persons.length(); j++) {
                     JSONObject grave= persons.getJSONObject(i);
                     lineArrayList.add(grave.getString("ID_line"));
                     //so on..
             }
          }

答案 3 :(得分:0)

好的只是今天我发现我只从第一个人阵列中获取数据,而第二个人的数据就是一个名为Mojca的人。我尝试了所有三个给定的解决方案,但没有任何作用..