英文:
Converting String to org.JSON does not maintain the order
问题
我有一个以下的 JSON:
"CustomError": {
"ErrorDesc": "Method Not Allowed",
"Error": "RaiseFault",
"ErrorCode": "405"
}
当我将它转换为 JSONObject 后,键值对的顺序被改变了。
这是我的代码段:
JSONObject policies = new JSONObject("\"CustomError\": {\n" +
" \"ErrorDesc\": \"Method Not Allowed\",\n" +
" \"Error\": \"RaiseFault\",\n" +
" \"ErrorCode\": \"405\"\n" +
" }");
policies.get("CustomError");
返回的是键值对的随机顺序。
我知道 org.JSON 内部使用的是 HashMap 而不是 LinkedHashMap。但是否有任何方法可以保持 CustomError 的键值对顺序呢?
英文:
I have a following JSON
"CustomError": {
"ErrorDesc": "Method Not Allowed",
"Error": "RaiseFault",
"ErrorCode": "405"
}
When I am converting it to JSONObject the order of the K-V are getting changed.
This is my piece of code
JSONObject policies = new JSONObject("\"CustomError\": {\n" +
" \"ErrorDesc\": \"Method Not Allowed\",\n" +
" \"Error\": \"RaiseFault\",\n" +
" \"ErrorCode\": \"405\"\n" +
" }");
policies.get("CustomError");
The returns the random order of K-V.
I know the org.JSON internally uses HashMap and not LinkedHashMap. But is there any way I can attain the order of K-V of my CustomError.
专注分享java语言的经验与见解,让所有开发者获益!
评论