英文:
How to custom order JSON string in Java
问题
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonOrderingExample {
public static void main(String[] args) {
String json = "{\n" +
" \"SignedSubscriptionRule\":{\n" +
" \"User rights\":\"something1\",\n" +
" \"Function\":\"something2\",\n" +
" \"Method name\":\"something3\"\n" +
" },\n" +
" \"CancelZeroSubscriptionRule\":{\n" +
" \"User rights\":\"something4\",\n" +
" \"Function\":\"something5\",\n" +
" \"Method name\":\"something6\"\n" +
" },\n" +
" \"NewLoanAllocationRule\":{\n" +
" \"User rights\":\"something7\",\n" +
" \"Function\":\"something8\",\n" +
" \"Method name\":\"something9\"\n" +
" }\n" +
"}";
String[] customOrder = {"SignedSubscriptionRule", "NewLoanAllocationRule", "CancelZeroSubscriptionRule"};
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode root = objectMapper.readTree(json);
String orderedJson = orderJson(root, customOrder);
System.out.println(orderedJson);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String orderJson(JsonNode root, String[] customOrder) {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode orderedNode = objectMapper.createObjectNode();
for (String key : customOrder) {
if (root.has(key)) {
orderedNode.set(key, root.get(key));
}
}
return orderedNode.toString();
}
}
Note: This code example uses the Jackson library to manipulate JSON objects in Java. The orderJson
function takes the original JSON node and the custom order as input and returns a new JSON node with the desired order. The Jackson library's ObjectNode
class is used to create and manipulate JSON objects.
英文:
I have a JSON string that looks something like this:
{
"SignedSubscriptionRule":{
"User rights":"something1",
"Function":"something2",
"Method name":"something3"
},
"CancelZeroSubscriptionRule":{
"User rights":"something4",
"Function":"something5",
"Method name":"something6"
},
"NewLoanAllocationRule":{
"User rights":"something7",
"Function":"something8",
"Method name":"something9"
}
}
Let's say I receive this JSON on daily basis and each time I have to reorder it to meet my needs.
How could I defined a custom order of this JSON. Let's say I want it to have the following order:
SignedSubscriptionRule, NewLoanAllocationRule, CancelZeroSubscriptionRule
I'm coding in Java.
I would like to avoid writing my own iterating solution.
答案1
得分: 0
只需注意,在JSON中的顺序除了一些罕见的情况外并没有意义。
例如:
{ "foo": "test", "bar": "test2" }
和
{ "bar": "test2", "foo": "test" }
是相同的。
顺便说一下,如果您需要对其进行排序,您需要编写一个比较器来设置排序规则,就像这样:
```java
Collections.sort(list, new Comparator() {
private static final String KEY_NAME = "Name";
@Override
public int compare(JSONObject a, JSONObject b) {
String str1 = new String();
String str2 = new String();
try {
str1 = (String)a.get(KEY_NAME);
str2 = (String)b.get(KEY_NAME);
} catch(JSONException e) {
e.printStackTrace();
}
return str1.compareTo(str2);
}
});
更多信息请参见:
https://www.tutorialspoint.com/how-can-we-sort-a-jsonarray-in-java
英文:
Just notice that ordering in JSON has no meaning except rare cases.
for example:
{ "foo": "test", "bar": "test2" }
and
{ "bar": "test2", "foo": "test" }
are the same.
by the way if you need to sort it, you need to write a comparator to set your rules for sorting, like this:
Collections.sort(list, new Comparator() {
private static final String KEY_NAME = "Name";
@Override
public int compare(JSONObject a, JSONObject b) {
String str1 = new String();
String str2 = new String();
try {
str1 = (String)a.get(KEY_NAME);
str2 = (String)b.get(KEY_NAME);
} catch(JSONException e) {
e.printStackTrace();
}
return str1.compareTo(str2);
}
});
more info on:
https://www.tutorialspoint.com/how-can-we-sort-a-jsonarray-in-java
专注分享java语言的经验与见解,让所有开发者获益!
评论