0

I have a bunch of JSON objects in a string notation:

"{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"ARNHEM\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}

So each of the JSON object looks something like this:

    {
        "address" : {
               "street" : "Steenstraat", 
               "housnumber" : "17A", 
               "postalcode" : "6828 CA", 
               "city" : "ARNHEM", 
               "geolocation" : {
                           "latitude" : "51.983718",
                           "longitude" : "54.983718"
                           }
                     },
               "type" : "citi",
               "distance" : 0
    }

Now, I used google's gson library to get from the rest API and that has given me a string of many of the above JSON objects. How can I try to filter out (or redefine the structure of the JSON) to sort the JSON objects by a particular parameter (say sort by city names)?

This is my Atm class. I'm trying to convert the JSON string to a list of Atm objects.

public class Atm {

    private String type;
    private Long distance;
    private Map<String, String> address = new HashMap<String, String>();

    public Atm() {
    }

    public String getType() {
        return type;
    }

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

    public Long getDistance() {
        return distance;
    }

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

    public Map<String, String> getAddress() {
        return address;
    }

    public void setAddress(Map<String, String> address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Atm{" +
                "type='" + type + 
                ", distance=" + distance +
                ", address=" + address.toString() +
                '}';
    }
}

Or is there a way to do this without converting it into java data structures?

Adway Dhillon
  • 77
  • 1
  • 4
  • 16

2 Answers2

0

Note: As you are using a subelement geoLocation in your JSON, you cannot use Map<String, String>. You should use: Map<String, Object> instead or create a custom class to represent your address.

To filter your Atm list by city you could do the following.

In Java 8:

Gson gson = new Gson();
String atm1 = "{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"ARNHEM\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}";
String atm2 = "{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"ARNHEM\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}";
String atm3 = "{\"address\":{\"street\":\"Steenstraat\",\"housenumber\":\"17A\",\"postalcode\":\"6828 CA\",\"city\":\"NEW-YORK\",\"geoLocation\":{\"lat\":\"51.983718\",\"lng\":\"5.915553\"}},\"distance\":0,\"type\":\"ING\"}";

List<Atm> atms = new ArrayList<Atm>();

atms.add(gson.fromJson(atm1, Atm.class));
atms.add(gson.fromJson(atm2, Atm.class));
atms.add(gson.fromJson(atm3, Atm.class));

List<Atm> filteredOnCity = atms.stream().filter(atm -> atm.getAddress().get("city")
    .equals("ARNHEM")).collect(Collectors.toList());

With Apache commons-collections4:

//Build your list with code from above
Predicate<Atm> filterOnCity = new Predicate<Atm>() {
    @Override
    public boolean evaluate(Atm atm) {
        return atm.getAddress().get("city").equals("ARNHEM");
    }
};

CollectionUtils.filter(atms, filterOnCity);
  • Can I somehow do this without the use of java? I keep getting this error when I try to make a list out of it: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path And I think I know why I'm getting this error. Its because I'm trying to have a list of Map, but the last element in the map is another map (the one for the geolocation) – Adway Dhillon Oct 17 '16 at 09:27
  • The easiest way to do this is to change your `Map` to `Map`. Then it will run straight out of the box. – Stijn Van Bever Oct 17 '16 at 09:50
0

Why not filter or sort them on the client side before sending them to the Java?

var arr = JSON.parse(MY_JSON_ARRAY_STRING);

arr.sort(function(a, b){
  if ( a.city < b.city )
        return -1;
    if ( a.city > b.city )
        return 1;
   return 0;
});

var arrString = JSON.stringify(arr);
MasNotsram
  • 2,105
  • 18
  • 28