如何获取主数组内子数组的值?

时间:2019-03-28 14:48:45

标签: java mysql json

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

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"
        }
     ]
  }
   ]
}

Java代码:

  public JSONArray result;
  public static final String JSON_ARRAY = "result";
  public static final String TAG_COMMONNAME= "commonname";
  public void getData(){

    //Creating a string request
    StringRequest stringRequests = new StringRequest(Config.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject jo = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        jo = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = jo.getJSONArray(JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getAnimals(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue2 = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue2.add(stringRequests);
}

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));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

0 个答案:

没有答案