2

I have a get request with the response of json like bellow:

{"version": 0.6,
 "generator": "Overpass API",
  "osm3s": {
  "timestamp_osm_base": "",
   "copyright": "The data included in this document is from  www.openstreetmap.org. The data is made available under ODbL."
 },
 "elements": [

{
 "type": "node",
 "id": 25941318,
 "lat": 35.7006285,
 "lon": 51.3909900
  },
 {
  "type": "node",
   "id": 26839944,
   "lat": 35.7006369,
   "lon": 51.3913739
    },
  {
   "type": "node",
   "id": 1333387625,
   "lat": 35.7012370,
   "lon": 51.3913564
    }

    ]
  }

I need to convert this Json to java object, this is my model class:

package com.findItntersection.model;

public class Intersection {

String type;

private long id;

private double lon;

private double lat;

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public double getLon() {
    return lon;
}

public void setLon(double lon) {
    this.lon = lon;
}

public double getLat() {
    return lat;
}

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

@Override
public String toString() {

      return "elements [ type=" + type + ",id=" + id + ",lon=" + lon +
      ".lat" + lat + "]";

}}

and my service method which sends get request correctly:

public void sendGet(String city, String street1, String street2)
        throws ClientProtocolException, IOException, URISyntaxException {

    Gson gson = new Gson();

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http")
            .setHost("192.168.0.67")
            .setPath("/api/interpreter")
            .setParameter(
                    "data",
                    "[out:json];area[name~\""
                            + city
                            + "\"]->.b;way(area.b)[highway][name~\""
                            + street1
                            + "\"];node(w)->.n1;way(area.b)[highway][name~\""
                            + street2 + "\"];node(w)->.n2;node.n1.n2;out;");

    URI uri = builder.build();

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(uri);
    CloseableHttpResponse response1 = httpclient.execute(httpGet);

    try {
        System.out.println(response1);
        HttpEntity entity1 = response1.getEntity();

        String json = EntityUtils.toString(entity1,"UTF-8");


        Intersection fromJson = gson.fromJson(json, Intersection.class);
        System.out.println(fromJson);
        EntityUtils.consume(entity1);
    } finally {
        response1.close();
    }
}

but the fromJson is like this: [ type=null,id=0,lon=0.0.lat0.0]

the result from get request json:

{
 "version": 0.6,
  "generator": "Overpass API",
   "osm3s": {
   "timestamp_osm_base": "",
   "copyright": "The data included in this document is from   www.openstreetmap.org. The data is made available under ODbL."
   },
  "elements": [

 {
  "type": "node",
   "id": 29004231,
   "lat": 35.7212341,
   "lon": 51.3888708
    }

    ]
   }

Can anybody help me please?

2
  • do you get the right result from ur get request? (i.e. do you get ur desired jsonObject) Commented Mar 11, 2015 at 11:22
  • yes,edited the post, result is correct Commented Mar 11, 2015 at 11:25

3 Answers 3

1

Gson cannot parse because your response is not a representation of Intersection class.

It represents something like that:

public class OsmResult implements Serializable{
    private double version;
    private String generator;
    private HashMap<String, String> osm3s;
    private ArrayList<Intersection> elements;

    ...getters & setters
}

From here, you can go two ways;

  1. Extract elements and parse it to a Intersection list.

I think there is no extraction method in Gson but you can try other Json libraries for that. Or, you can do simple String operations to extract elements.

  1. Parse it to a class like above and just use getElements() method.

If you stick with Gson, I recommend that as it is more cleaner way to do it.

Sign up to request clarification or add additional context in comments.

Comments

1

That's because your response is not an Intersection. It is an object which contains an array of Intersections.

So, you first need to convert the JSON to a JsObject, then get this array and convert it to List of Intersections.

String json = EntityUtils.toString(entity1,"UTF-8");

JsonParser jp = new JsonParser();

JsonObect jo = ( JsonObject ) jp.parse( json );

// JsonArray of intersection JsonElement's
JsonArray ija = ( JsonArray ) jo.get( "elements" );

// Iterator of intersection JsonElements
Iterator<JsonElement> iji = ija.iterator();

List< Intersection> il = new ArrayList< Intersection >();

while( iji.hasNext() ) {
    JsonElement je = iji.next();
    // convert to Intersection
    Intersection i = gson.fromJson( je, Intersection.class );
    // add to List
    il.add( i );
}

// do whatever you want with your list of intersections.

Comments

0

This is because your model class is not correct. Class Intersection just maps the elements in json. You should have a model class which maps all the json elements , with "Elements" as another class defining its own elements.

http://www.journaldev.com/2324/jackson-json-processing-api-in-java-example-tutorial

This example should help.

1 Comment

I have also tried this in my model: @Override public String toString() { return " {\n \"version\": 06,\n \"generator\": \"Overpass API\",\n \"osm3s\" : {\n \"timestamp_osm_base\": \"\" ,\n \"copyright\": \"The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.\"\n}," + "\n \"elements\" : [\n\n{\n \"type\" : " + type + ",\n \"id\" : " + id + ",\n \"lat\" : " + lat + ",\n \"lon\" : " + lon + "}]}";} but didn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.