将字符串转换为org.JSON,不会保持顺序。

huangapple 未分类评论48阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年5月5日 04:37:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61601161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定