0

I have a json string something similar

{"results":
[{"_type":"Position","_id":377078,"name":"Potsdam, Germany","type":"location","geo_position":{"latitude":52.39886,"longitude":13.06566}},
{"_type":"Position","_id":410978,"name":"Potsdam, USA","type":"location","geo_position":{"latitude":44.66978,"longitude":-74.98131}}]}

I am trying to convert to

    {"results":
 [{"_type":"Position","_id":377078,"name":"Potsdam, Germany","type":"location","latitude":52.39886,"longitude":13.06566}, 
{"_type":"Position","_id":410978,"name":"Potsdam, USA","type":"location","latitude":44.66978,"longitude":-74.98131}]}

I am converting to java and again converting back using But I am gettin null in data

  SourceJSON    data=new Gson().fromJson(jsonArray, SourceJSON.class);
           DestinationJSON destdata = new DestinationJSON();
           destdata.setLatitide(data.getGeoLocation().getLatitide());
           destdata.setLongitude(data.getGeoLocation().getLongitude());
           destdata.setId(data.getId());
           destdata.setType(data.getType());
           destdata.setName(data.getName());
           destdata.set_type(data.get_type());


           Gson gson = new Gson();
           String json = gson.toJson(destdata);

below are my beans

public class SourceJSON implements Serializable {
    private List<GEOLocation> geoLocations;
    private String  _type;
    private String id;
    private String name;
    private String type;


    public String get_type() {
        return _type;
    }
    public List<GEOLocation> getGeoLocations() {
        return geoLocations;
    }
    public void setGeoLocations(List<GEOLocation> geoLocations) {
        this.geoLocations = geoLocations;
    }
    public void set_type(String _type) {
        this._type = _type;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }



}

and

public class GEOLocation implements Serializable{
    private String latitide;
    private String longitude;
    public String getLatitide() {
        return latitide;
    }
    public void setLatitide(String latitide) {
        this.latitide = latitide;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }


}

and destination java

public class DestinationJSON  implements Serializable {

    private String  _type;
    private String id;
    private String name;
    private String type;
    private String latitide;
    private String longitude;
    public String get_type() {
        return _type;
    }
    public void set_type(String _type) {
        this._type = _type;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getLatitide() {
        return latitide;
    }
    public void setLatitide(String latitide) {
        this.latitide = latitide;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}
4
  • Do you need only to change the JSON or to store data into the classes? Commented Dec 6, 2013 at 21:06
  • I need to change the json this is one way i thought I can change but if you have any better way? Commented Dec 6, 2013 at 21:08
  • I do not see SourceJSON#getGeoLocation() method :-/ Commented Dec 6, 2013 at 21:13
  • My final output is I want to change the json format Commented Dec 6, 2013 at 21:15

1 Answer 1

1

All you need is this. You can try this class in your IDE with a simple copy&paste.

package stackoverflow.questions;

import java.util.*;

import com.google.gson.Gson;

public class Q20433539{

   public static void main(String[] args){
         String json = "{\"results\":"+
         "[{\"_type\":\"Position\",\"_id\":377078,\"name\":\"Potsdam, Germany\",\"type\":\"location\",\"geo_position\":{\"latitude\":52.39886,\"longitude\":13.06566}},"+
         "{\"_type\":\"Position\",\"_id\":410978,\"name\":\"Potsdam, USA\",\"type\":\"location\",\"geo_position\":{\"latitude\":44.66978,\"longitude\":-74.98131}}]}";


   Gson gson = new Gson();
   Map m = gson.fromJson(json, Map.class);

   List<Map> innerList = (List<Map>) m.get("results");
   for(Map result: innerList){
      Map<String, Double> geo_position = (Map<String, Double>) result.get("geo_position");
      result.put("latitude", geo_position.get("latitude"));
      result.put("longitude", geo_position.get("longitude"));
      result.remove("geo_position");
   }
   System.out.println(gson.toJson(m));


   }


}

Of course, it works under the assumption that you always want to flat geo information.

Explanation: It's convenient to use POJO when working with Gson, but it's not the only way. Gson can also deseralize to Arrays/Maps if you do not specify the expected result. So I did, and then I manipulated the structure to unfold your data. After that, Gson can serialize Arrays/Maps structure again to your desidered JSON.

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

1 Comment

Welcome. Btw, nice nickname. It reminds that we are here to learn from each other.

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.