开发者

What's the proper representation of LinkedHashMap in JSON?

开发者 https://www.devze.com 2023-02-18 06:32 出处:网络
When I serialize my LinkedHashMap<String, String> using GSON, I get {\"b\":\"a\",\"a\":\"1\",\"c\":\"2\"}

When I serialize my LinkedHashMap<String, String> using GSON, I get

{"b":"a","a":"1","c":"2"}

and after deserializing I get back the elements in the right or开发者_JAVA技巧der. So everything works fine, but is there a guarantee that every tool works this way? Does the order of entries have any significance in JSON?


No, Javascript hashes do not necessarily have a consistent order. For that, you'd probably want a representation like

[["b", "a"], ["a", "1"], ["c", "2"]]

or store the key order in a separate list, depending on what you are doing with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号