简体   繁体   中英

Filter JSON Objects by values

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?

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM