[英]Sorting JSONObject using a value of a JSONArray in Java
I have following JSONObject i am trying to sort:我有以下 JSONObject 我正在尝试排序:
{
"coords": {
"27": {
"x": [70,71,72],
"y": [1945,1946,1947],
"lineNumber": [
19
]
},
"29": {
"x": [70,71,72],
"y": [1945,1946,1947],
"lineNumber": [
91
]
},
"2333": {
"x": [70,71,72],
"y": [1945,1946,1947],
"lineNumber": [
10
]
}
}
}
I wanted to sort the JSONObjects inside coords
wrt the value lineNumber
.我想根据值
lineNumber
对coords
内的 JSONObjects 进行排序。 lineNumber
array always has only one element inside. lineNumber
数组内部始终只有一个元素。 I am using simple JSON.我正在使用简单的 JSON。
Unfortunately there isn't a clean way to do this.不幸的是,没有一种干净的方法可以做到这一点。
You can convert your map to a list of Map.Entry<K,V>
objects.您可以将地图转换为
Map.Entry<K,V>
对象列表。 In your case the entries are of type Map.Entry<String, JSONObject>
(entries inside coords
).在您的情况下,条目的类型为
Map.Entry<String, JSONObject>
(在coords
内的条目)。
The list can then be sorted by entry V
alues, or in your case JSONObject
s.该列表然后可以通过入口进行排序
V
alues,或在您的案件JSONObject
秒。 The easiest way to do this is to write a custom Comparator and sort your list via Collections.sort(...) .最简单的方法是编写一个自定义Comparator并通过Collections.sort(...)对您的列表进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.