【问题标题】:How to get data form JSON with multiple objects and arrays如何从具有多个对象和数组的 JSON 中获取数据
【发布时间】:2017-09-26 07:19:44
【问题描述】:

我是编程新手,我想构建一个从谷歌地图方向获取数据的应用程序,第一个用户需要在列表视图中获取可用路线。

我的 JSON 数据是这样的

{
  geocoded_waypoints: [],
  routes: [
    {
      bounds: {

  },
  copyrights: "Map data ©2017 Google",
  legs: [
    {
      arrival_time: {},
      departure_time: {},
      distance: {},
      duration: {
        text: "25 mins",
        value: 1514
      },
      end_address: "destination address, Croatia",
      end_location: {},
      start_address: "start address",
      start_location: {},
      steps: [
        {
          distance: {},
          duration: {},
          end_location: {},
          html_instructions: "Walk to Vjesnik",
          polyline: {},
          start_location: {},
          steps: [],
          travel_mode: "WALKING"
        },
        {
          distance: {},
          duration: {},
          end_location: {},
          html_instructions: "Tram towards Dubec",
          polyline: {},
          start_location: {},
          transit_details: {
            arrival_stop: {},
            arrival_time: {},
            departure_stop: {},
            departure_time: {},
            headsign: "Dubec",
            line: {
              agencies: [
                {
                  name: "Zagrebački Električni Tramvaj",
                  phone: "011 385 60 100 001",
                  url: "http://www.zet.hr/"
                }
              ],
              color: "#ffffff",
              name: "Savski most - Dubec",
              short_name: "4",
              text_color: "#400000",
              vehicle: {}
            },
            num_stops: 9
          },
          travel_mode: "TRANSIT"
        },

这是数组中的第一条路线,

我确实找到了一些代码,但我没有剪切这是正确的,我需要去

duration: {
text: "25 mins",
value: 1514
},
  • 这是在腿数组中

然后在 steps 数组中,我需要对象 transit_details 女巫有 2 个对象行和 num_stops,在对象行中,我有对象:名称、短名称和数组机构。

我不确定这是否是构成我的情况的正确代码,并且我意识到我需要获取两个数组,父母和孩子。 如果这个问题已经被问过,我很抱歉,但您能指导我如何获取这些数据吗?

我发现的Java代码是这样的,但我确实修改了它并放了一些cmets:

   public class DirectionsJSONParser {

    /** Receives a JSONObject and returns a list of lists containing latitude and longitude */
    public List<List<HashMap<String,String>>> parse(JSONObject jObject){

        List<List<HashMap<String, String>>> routes = new ArrayList<List<HashMap<String,String>>>() ;
        JSONArray jRoutes = null;
        JSONArray jLegs = null;
        JSONObject jSteps = null;

        try {

            jRoutes = jObject.getJSONArray("routes");

            /** Traversing all routes */
            for(int i=0;i<jRoutes.length();i++){
                jLegs = ( (JSONObject)jRoutes.get(i)).getJSONArray("legs");
                List path = new ArrayList<HashMap<String, String>>();


                /** Traversing all legs */
                for(int j=0;j<jLegs.length();j++){
                    jSteps = ( (JSONObject)jLegs.get(j)).getJSONArray("steps");
                    jLegs = ((JSONObject)jRoutes.get(j)).getJSONArray("duration"); // --> this is my 

                    /** Traversing all steps */
                    for(int k=0;k<jSteps.length();k++){
                        String polyline = "";
                        polyline = (String)((JSONObject)((JSONObject)jSteps.get(k)).get("polyline")).get("points");
                        List list = decodePoly(polyline);
                        jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("transit_details"); // this is my
                        jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("line"); // this is my
                        jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("name"); // this is my
                        jSteps = ((JSONObject)jSteps.get(k)).getJSONObject("short_name"); // this is my

                    }
                    routes.add(path);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }catch (Exception e){
        }

        return routes;
    }

    /**
     * Method to decode polyline points
     * Courtesy : http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java
     * */
    private List decodePoly(String encoded) {

        List poly = new ArrayList();
        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((((double) lat / 1E5)),
                    (((double) lng / 1E5)));
            poly.add(p);
        }

        return poly;
    }
}

我没有得到我需要的数据。

谢谢你帮助我。

【问题讨论】:

标签: java android arrays json


【解决方案1】:

您可以使用像 gson 这样的 JSON-API 来解析 Java 中的 JSON 数据

【讨论】:

  • 好的,我确实找到了 gson,我用代码获取了数据:
【解决方案2】:

好的,我用 gson 获取数据,但是如何将它们放在列表视图中 我的代码是:

private void extractTransitDetails(TransitDetailsWrapper transitDetailsWrapper) {
    List<Route> routes = transitDetailsWrapper.getRoutes();
    routes.size();
    for (Route route  : routes) {
        List<Leg> legs = route.getLegs();
        route.getOverviewPolyline();
        for (Leg leg  : legs) {
            List<Step> steps = leg.getSteps();
            leg.getDuration(); // vrijeme putovanja
            leg.getDistance(); // udaljenost u km
            for (Step step : steps) {
                TransitDetails transitDetails = step.getTransitDetails();

                if (transitDetails != null) {
                    transitDetails.getLine();
                    if (transitDetails.getLine() != null) {
                        transitDetails.getLine().getShortName();
                        transitDetails.getLine().getName();
                    }
                    transitDetails.getNumStops();
                    transitDetails.getArrivalStop().getName();
                    transitDetails.getDepartureStop().getName();
                    transitDetails.getLine().getAgencies();

                }

                Log.d("test", "majlajs");

            }
        }
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2021-07-24
    • 2019-01-06
    • 2016-10-15
    • 1970-01-01
    • 2021-11-24
    • 2022-11-04
    • 2021-11-07
    相关资源
    最近更新 更多