如何解析这个json响应?

时间:2010-08-28 20:10:17

标签: json java-me

我知道如何解析响应,例如{\“screenTitle \”:\“National Geographic \”} token.optString( “screenTitle”);直到这个确定。

但看到此回复

{
  "status": "OK",
  "routes": [ {
    "summary": "Southern Exp",
    "legs": [ {
      "steps": [ {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": -34.9257700,
          "lng": 138.5997300
        },
        "end_location": {
          "lat": -34.9106200,
          "lng": 138.5991300
        },

我想要lat和long值。 我怎么能做到这一点。

4 个答案:

答案 0 :(得分:2)

以下是在以下路径中获取start_location的纬度和经度的快速代码 routes[0]/legs[0]/steps[0]/start_location

   JSONObject obj = new JSONObject(source.toString());
   JSONObject startLoaction = obj.getJSONArray("routes")
      .getJSONObject(0).getJSONArray("legs")
      .getJSONObject(0).getJSONArray("steps")
      .getJSONObject(0).getJSONObject("start_location");
   System.out.println(
     startLoaction.get("lat") + ", " + startLoaction.get("lng")
   );

此代码只是为了让您了解如何在JSON深层解析对象。您可能希望为JSON对象编写一个通用路径,例如查询机制(想想类似于XPath),这将基于JSON内的查询为您提供对象

e.g。在你的情况下:

JSONObject startLocation = JSONFinder.query(
      "/routes[0]/legs[0]/steps[0]/start_location", sourceJSONObject)

答案 1 :(得分:1)

没有什么能够解析你提供的字符串,因为它有语法错误。

但是,这是JSON。按照您最喜欢的支持JSON in Java的工具的说明进行操作。

您是否能够提供正在引发的异常?然后我可以写一些更具体的内容。

答案 2 :(得分:1)

使用以下依赖

 <!-- json -->
            <dependency>
            <groupId>com.google.code.geocoder-java</groupId>
            <artifactId>geocoder-java</artifactId>
            <version>0.9</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>

在主要方法中使用以下代码

public static void main(String[] args) {
        try {
            String point = "your json response";
            Gson gson = new Gson();
            TypeToken<RoutePlot> tokenPoint = new TypeToken<RoutePlot>() {
                };

            RoutePlot user = gson.fromJson(point, tokenPoint.getType());
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

现在使用以下类,我已生成响应映射到java

package com.admin.modules.restservices.google.pojo.routepojo;

public class Bounds {
    private Northeast northeast;
    private Southwest southwest;

    public Northeast getNortheast() {
        return northeast;
    }

    public void setNortheast(Northeast northeast) {
        this.northeast = northeast;
    }

    public Southwest getSouthwest() {
        return southwest;
    }

    public void setSouthwest(Southwest southwest) {
        this.southwest = southwest;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Distance {
    private String text;
    private String value;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Duration {
    private String text;
    private String value;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Endlocation {
    private String lat;
    private String lng;

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

import java.util.List;


public class Legs {
    private Distance distance;
    private Duration duration;
    private String end_address;
    private Endlocation end_location;
    private String start_address;
    private Startlocation start_location;
    private List<Steps> steps;
    private List<String> via_waypoint;

    public List<String> getVia_waypoint() {
        return via_waypoint;
    }

    public void setVia_waypoint(List<String> via_waypoint) {
        this.via_waypoint = via_waypoint;
    }

    public Distance getDistance() {
        return distance;
    }

    public void setDistance(Distance distance) {
        this.distance = distance;
    }

    public Duration getDuration() {
        return duration;
    }

    public void setDuration(Duration duration) {
        this.duration = duration;
    }

    public String getEnd_address() {
        return end_address;
    }

    public void setEnd_address(String end_address) {
        this.end_address = end_address;
    }

    public Endlocation getEnd_location() {
        return end_location;
    }

    public void setEnd_location(Endlocation end_location) {
        this.end_location = end_location;
    }

    public String getStart_address() {
        return start_address;
    }

    public void setStart_address(String start_address) {
        this.start_address = start_address;
    }

    public Startlocation getStart_location() {
        return start_location;
    }

    public void setStart_location(Startlocation start_location) {
        this.start_location = start_location;
    }

    public List<Steps> getSteps() {
        return steps;
    }

    public void setSteps(List<Steps> steps) {
        this.steps = steps;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Northeast {
    private String lat;
    private String lng;
    public String getLat() {
        return lat;
    }
    public void setLat(String lat) {
        this.lat = lat;
    }
    public String getLng() {
        return lng;
    }
    public void setLng(String lng) {
        this.lng = lng;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Overviewpolyline {

}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Polyline {
    private String points;

    public String getPoints() {
        return points;
    }

    public void setPoints(String points) {
        this.points = points;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

import java.util.List;


public class RoutePlot {
    private String status;
    private List<Routes> routes;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public List<Routes> getRoutes() {
        return routes;
    }

    public void setRoutes(List<Routes> routes) {
        this.routes = routes;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

import java.util.List;


public class Routes {
    private Bounds bounds;
    private String copyrights;
    private List<Legs> legs;
    private Overviewpolyline overview_polyline;
    private String summary;
    private List<String> warnings;
    private List<String> waypoint_order;

    public Bounds getBounds() {
        return bounds;
    }

    public void setBounds(Bounds bounds) {
        this.bounds = bounds;
    }

    public String getCopyrights() {
        return copyrights;
    }

    public void setCopyrights(String copyrights) {
        this.copyrights = copyrights;
    }

    public List<Legs> getLegs() {
        return legs;
    }

    public void setLegs(List<Legs> legs) {
        this.legs = legs;
    }

    public Overviewpolyline getOverview_polyline() {
        return overview_polyline;
    }

    public void setOverview_polyline(Overviewpolyline overview_polyline) {
        this.overview_polyline = overview_polyline;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public List<String> getWarnings() {
        return warnings;
    }

    public void setWarnings(List<String> warnings) {
        this.warnings = warnings;
    }

    public List<String> getWaypoint_order() {
        return waypoint_order;
    }

    public void setWaypoint_order(List<String> waypoint_order) {
        this.waypoint_order = waypoint_order;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Southwest {
private String lat;
private String lng;
public String getLat() {
    return lat;
}
public void setLat(String lat) {
    this.lat = lat;
}
public String getLng() {
    return lng;
}
public void setLng(String lng) {
    this.lng = lng;
}


}
package com.admin.modules.restservices.google.pojo.routepojo;

public class Startlocation {
    private String lat;
    private String lng;

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }
}
package com.admin.modules.restservices.google.pojo.routepojo;

import java.util.List;

public class Steps {
        private Distance distance;
        private Duration duration;

        private Endlocation end_location;

        private Startlocation start_location;
        private String html_instructions;
        private String travel_mode;
        private Polyline polyline;

}

答案 3 :(得分:0)

好的,看到你有一个JSON字符串(你可能正在使用here中的JSON),如果你想从JSON返回一个数组,那就做这样的事情:

JSONArray array = json.getJSONArray("routes");

编辑要获取地理位置,您必须遍历数组。在此示例中,我从routs[0]/legs[0]/steps[0]/start_location

中获取了值
JSONArray array = json.getJSONArray("routes");

for (int i = 0; i < array.length(); i++) {
    JSONObject subJson = array.getJSONObject(i); //Assuming that in the array there's ONLY JSON objects
    JSONArray legs = subJson.getJSONArray("legs");

    //Now for legs, you can iterate through it but I don't know how your JSON structure is so
    //I didn't do it here.
    JSONArray steps = legs.getJSONObject(0).getJSONArray("steps");
    JSONObject startLocation = steps.getJSONObject(0).getJSONObject("start_location");

    double lat = startLocation.getDouble("lat");
    double lng = startLocation.getDouble("lng");
}
相关问题