【问题标题】:How do i preserve the original order of json with JSONArray如何使用 JSONArray 保留 json 的原始顺序
【发布时间】:2017-05-27 19:39:35
【问题描述】:

我正在使用 JSONArray 对象,并将字符串传递给该对象的构造函数。我传递的字符串是

[{\"x\":18.4300,\"y\":30.4700,\"w\":53.0900,\"fontSize\": 11,\"bold\": 0,\"charcount\": 22,\"id\": 349133}]

输出 json 对象后,我得到以下信息:

[{"charcount":22,"w":53.09,"x":18.43,"y":30.47,"fontSize":11,"bold":0,"id":349133}]

我能否在代码中获得一个示例,说明如何保留原始 json 字符串的顺序?

【问题讨论】:

标签: java json


【解决方案1】:

您可以使用有序集合来解析 json 以保持其顺序。 您的 json 示例:

    String json = "[{\"x\":18.4300,\"y\":30.4700,\"w\":53.0900,\"fontSize\": 11,\"bold\": 0,\"charcount\": 22,\"id\": 349133}]";
    Gson gson = new Gson();
    Type type = new TypeToken<Set<LinkedTreeMap<String, Object>>>() {}.getType();
    Set<LinkedTreeMap<String, Object>> myMap = gson.fromJson(json, type);
    System.out.println(json);
    System.out.println(myMap);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多