[英]Filter JSON Objects by values
I have a bunch of JSON objects in a string notation: 我有一堆字符串符号形式的JSON对象:
"{\"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: 因此,每个JSON对象如下所示:
{
"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. 现在,我使用google的gson库从其余的API中获取信息,这给了我许多上述JSON对象的字符串。 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)?
如何尝试滤除(或重新定义JSON的结构)以按特定参数对JSON对象进行排序(例如按城市名称排序)?
This is my Atm class. 这是我的Atm课程。 I'm trying to convert the JSON string to a list of Atm objects.
我正在尝试将JSON字符串转换为Atm对象列表。
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? 还是有一种方法可以不将其转换为Java数据结构?
Note: As you are using a subelement geoLocation
in your JSON, you cannot use Map<String, String>
. 注意:在JSON中使用子元素
geoLocation
时,不能使用Map<String, String>
。 You should use: Map<String, Object>
instead or create a custom class to represent your address. 您应该使用:
Map<String, Object>
或创建一个自定义类来表示您的地址。
To filter your Atm
list by city you could do the following. 要按城市过滤自动
Atm
列表,可以执行以下操作。
In Java 8: 在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: 使用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? 在将它们发送到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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.