数组内的数组-Java代码

时间:2019-03-29 11:03:56

标签: java arrays json parsing

我是编程新手。我有从名为“ subresult”的子数组中获取值的问题。我有从数组获取值的代码,但似乎它只获取外部数组而忽略内部数组。可惜我没有运气。请尊重我的问题,因为我只是一个初学者。希望你们能回答。谢谢!

products.sort((a,b)=> a['PendingQuantity'] - b['PendingQuantity']);

Java代码:

JSON:

{  
   "result":[  
      {  
         "commonname":"Tamaraw",
         "subresult":[  
            {  
           "latitude":"13.088847376649245",
           "longitude":"121.0535496566772",
           "province":"Mindoro"
        },
        {  
           "latitude":"14.898346071682198",
           "longitude":"121.42616213302608",
           "province":"General nakar"
        },
        {  
           "latitude":"14.44133541176629",
           "longitude":"120.45936525802608",
           "province":"Bataan"
        }
     ]
  },
  {  
     "commonname":"Philippine Tarsier",
     "subresult":[  
        {  
           "latitude":"9.810806171435631",
           "longitude":"124.26143093023506",
           "province":"Bohol"
        }
     ]
  },
  {  
     "commonname":"Philippine Eagle",
     "subresult":[  
        {  
           "latitude":"7.2396901503428",
           "longitude":"125.44315069027664",
           "province":"Davao City"
        }
     ]
  },
  {  
     "commonname":"Visayan Warty Pig",
     "subresult":[  
        {  
           "latitude":"9.651642644962154",
           "longitude":"122.84131398239595",
           "province":"Panay And Negros"
        }
     ]
  },
  {  
     "commonname":"Philippine Fresh Water Crocodile",
     "subresult":[  
        {  
           "latitude":"13.093068957060206",
           "longitude":"121.06598892373722",
           "province":"Mindoro"
        }
     ]
  },
  {  
     "commonname":"Walden's Hornbill",
     "subresult":[  
        {  
           "latitude":"10.071930427284427",
           "longitude":"125.59779391691245",
           "province":"Camiguin Sur and Dinagat Island"
        },
        {  
           "latitude":"14.656674396646768",
           "longitude":"121.05812014083858",
           "province":"Quezon"
        }
     ]
  },
  {  
     "commonname":"Philippine Cockatoo",
     "subresult":[  
        {  
           "latitude":"9.380944735295179",
           "longitude":"118.38456063371927",
           "province":"Palawan"
        },
        {  
           "latitude":"15.491670747921509",
           "longitude":"120.94052188562534",
           "province":"Cabanatuan City"
        },
        {  
           "latitude":"15.378556159943873",
           "longitude":"121.39594973068233",
           "province":"Dingalan"
        }
     ]
  },
  {  
     "commonname":"Negros Bleeding-heart",
     "subresult":[  
        {  
           "latitude":"9.551081019434731",
           "longitude":"123.09185859510103",
           "province":"Negros and Panay"
        }
     ]
  },
  {  
     "commonname":"Philippine naked-backed fruit bat",
     "subresult":[  
        {  
           "latitude":"9.646007526813666",
           "longitude":"122.85255472090171",
           "province":"Negros"
        }
     ]
  },
  {  
     "commonname":"Philippine Forest Turtle",
     "subresult":[  
        {  
           "latitude":"9.35368808473656",
           "longitude":"118.36544272849846",
           "province":"Palawan"
        }
     ]
  },
  {  
     "commonname":"Dinagat bushy-tailed Cloud Rat",
     "subresult":[  
        {  
           "latitude":"10.100726050965035",
           "longitude":"125.59963979398412",
           "province":"Dinagat Island"
        }
     ]
  },
  {  
     "commonname":"Hawksbill Sea Turtle",
     "subresult":[  
        {  
           "latitude":"6.984796116278719",
           "longitude":"122.02642351890961",
           "province":"Zamboanga"
        }
     ]
  },
  {  
     "commonname":"Philippine Spotted Deer",
     "subresult":[  
        {  
           "latitude":"16.229997551983594",
           "longitude":"120.52623997214562",
           "province":"Baguio"
        }
     ]
  }
   ]
}

1 个答案:

答案 0 :(得分:1)

public void getAnimals(JSONArray ja){
    //Traversing through all the items in the json array
    for(int i=0;i<ja.length();i++){
        try {
            //Getting json object
            json = ja.getJSONObject(i);

            //Adding the name of the student to array list
            students.add(json.getString(TAG_COMMONNAME));

            //TRY THIS
            JsonArray subResultJsonArray = json.optJSONArray("subresult");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }