如何从嵌套数组中提取 JSON 数据?使用 android

How to extract the JSON data from the nested arrays? using android

这是我的 JSON 结果,我需要在持续时间数组中获取 text。如何获得?

{
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 19.0759817,
               "lng" : 80.27071789999999
            },
            "southwest" : {
               "lat" : 12.5100381,
               "lng" : 72.8776544
            }
         },
         "copyrights" : "Map data ©2015 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "1,334 km",
                  "value" : 1333618
               },
               "duration" : {
                  "text" : "17 hours 43 mins",
                  "value" : 63762
               },
               "end_address" : "Mumbai, Maharashtra, India",
               "end_location" : {
                  "lat" : 19.0759817,
                  "lng" : 72.8776544
               },
               "start_address" : "Chennai, Tamil Nadu, India",
               "start_location" : {
                  "lat" : 13.0826782,
                  "lng" : 80.27071789999999
               },

我已经尝试使用此代码,因此我可以获得整个 distance 数组,但我只需要其中的 text 元素。

  JSONArray one = reader.getJSONArray("routes");

              for (int i = 0; i < one.length(); i++) {

                JSONObject two = one.getJSONObject(i);

                JSONArray three = two.getJSONArray("legs");
                for (int j = 0; j < three.length(); j++) {

                  JSONObject four = three.getJSONObject(j);

                     String str_long_name=four.getString("distance");

                 }

              }  

所以我试着像这样修改代码

 JSONArray one = reader.getJSONArray("routes");

      for (int i = 0; i < one.length(); i++) {

        JSONObject two = one.getJSONObject(i);

        JSONArray three = two.getJSONArray("legs");
        for (int j = 0; j < three.length(); j++) {

          JSONObject four = three.getJSONObject(j);
          JSONArray five = four.getJSONArray("distance");
          for (int  k= 0; k < five.length(); k++) {

              JSONObject six = five.getJSONObject(k);
              String str_long_name=six.getString("text");

          }                  

            }

      }

但它不起作用。它也没有显示任何错误。

JSONArray five = four.getJSONArray("distance");是错误的。 "distance"JSONObject.

试试下面的代码。

JSONObject five = four.getJSONObject("distance");
String str_long_name=five.getString("text");
System.out.println(str_long_name);

这里是完整的测试代码

import org.json.JSONArray;
import org.json.JSONObject;

public class Test {

public static void main(String[] args) {
    String str = "{\"routes\":[{\"bounds\":{\"northeast\":{\"lat\":19.0759817,\"lng\":80.27071789999999},\"southwest\":{\"lat\":12.5100381,\"lng\":72.8776544}},\"copyrights\":\"Map data ©2015 Google\",\"legs\":[{\"distance\":{\"text\":\"1,334 km\",\"value\":1333618},\"duration\":{\"text\":\"17 hours 43 mins\",\"value\":63762},\"end_address\":\"Mumbai, Maharashtra, India\",\"end_location\":{\"lat\":19.0759817,\"lng\":72.8776544},\"start_address\":\"Chennai, Tamil Nadu, India\",\"start_location\":{\"lat\":13.0826782,\"lng\":80.27071789999999}}]}]}";
    JSONObject reader = new JSONObject(str);
    JSONArray one = reader.getJSONArray("routes");

    for (int i = 0; i < one.length(); i++) {

      JSONObject two = one.getJSONObject(i);

      JSONArray three = two.getJSONArray("legs");
      for (int j = 0; j < three.length(); j++) {

        JSONObject four = three.getJSONObject(j);
        JSONObject five = four.getJSONObject("distance");
        String str_long_name=five.getString("text");
        System.out.println(str_long_name);

          }

    }
}
}

我前段时间上传了一个 class 来解析 google 地图中的路线 请检查它;) https://github.com/tato469/Android/blob/master/routes%20googleMaps%20v2/Route.java