java - 在 Java 中映射到 JSON

标签 java json

我有一张这样的 map map :

{ 
  facility-1={
    facility-kind1={param1=XPath-1, param2=XPath-2}, 
    facility-kind2={param1=XPath-1, param2=XPath-2}, 
    facility-kind3={param1=XPath-1, param2=XPath-2}
  },
  facility-2={
    facility-kind1={param1=XPath-1, param2=XPath-2}, 
    facility-kind2={param1=XPath-1, param2=XPath-2}, 
    facility-kind3={param1=XPath-1, param2=XPath-2}
  }
}

我想将其转换为这样的 JSON 格式

[

    {"title": "Item 1"},
    {"title": "Folder 2",
        "children": [
            {"title": "Sub-item 2.1"},
            {"title": "Sub-item 2.2"}
        ]
    },
    {"title": "Folder 3",
        "children": [
            {"title": "Sub-item 3.1"},
            {"title": "Sub-item 3.2"}
        ]
    },
    {"title": "Item 5"}
]

我尝试使用 GSON 但结果输出不是我想要的:

{
  "facility-1": {
     "facility-kind1":
      {"param1":"XPath-1","param2":"XPath-2"},
     "facility-kind2":
      {"param1":"XPath-1","param2":"XPath-2"},
     "facility-kind3":
      {"param1":"XPath-1","param2":"XPath-2"}
  },
  "facility-2": { 
     "facility-kind1":
      {"param1":"XPath-1","param2":"XPath-2"},
     "facility-kind2":
      {"param1":"XPath-1","param2":"XPath-2","param3":"XPath-3"},
     "facility-kind3":
      {"param1":"XPath-1","param2":"XPath-2"}
  }
}

如何获得我想要的格式的json?

最佳答案

您需要将 JSON 转换为您提供的新格式。

要转换的数据:

static String json = 
    "{\n" + 
    "  facility-1={\n" + 
    "    facility-kind1={param1=XPath-1, param2=XPath-2},\n" +  
    "    facility-kind2={param1=XPath-1, param2=XPath-2},\n" + 
    "    facility-kind3={param1=XPath-1, param2=XPath-2}\n" + 
    "  },\n" + 
    "  facility-2={\n" + 
    "    facility-kind1={param1=XPath-1, param2=XPath-2},\n" +  
    "    facility-kind2={param1=XPath-1, param2=XPath-2},\n" +  
    "    facility-kind3={param1=XPath-1, param2=XPath-2}\n" + 
    "  }\n" + 
    "}\n";

使用 GSON

创建一些您想要处理数据的类,它们看起来像:

static class Facility {
    List<Kind> children = new LinkedList<Kind>();
}

static class Kind {
    String title;
    Map<String, String> params;

    public Kind(String title, Map<String, String> params) {
        this.title = title;
        this.params = params;
    }
}

下一步是查看源代码并创建它的表示。我会使用:

Map<String, Map<String, Map<String, String>>>

因为输入数据是这样布局的。现在使用 Gson 转换它非常简单:

public static void main(String... args) throws Exception {

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    Type type = new TypeToken<
            Map<String, Map<String, Map<String, String>>>>() {}.getType();

    Map<String, Map<String, Map<String, String>>> source = 
        gson.fromJson(json, type);

    Map<String, Facility> dest = new HashMap<String, Facility>();

    for (String facilityName : source.keySet()) {
        Map<String, Map<String, String>> facility = source.get(facilityName);

        Facility f = new Facility();

        for (String kindName : facility.keySet())
            f.children.add(new Kind(kindName, facility.get(kindName)));

        dest.put(facilityName, f);
    }

    System.out.println(gson.toJson(dest));
}
<小时/>

使用 JSONObject/JSONArray
public static void main(String... args) throws Exception {

    JSONObject source = new JSONObject(json);
    JSONArray destination = new JSONArray();

    for (Iterator<?> keys = source.keys(); keys.hasNext(); ) {

        String facilityName = (String) keys.next();
        JSONObject kinds = source.getJSONObject(facilityName);

        JSONArray children = new JSONArray();
        for (Iterator<?> kit = kinds.keys(); kit.hasNext(); ) {

            String kind = (String) kit.next();
            JSONObject params = kinds.getJSONObject(kind);

            JSONObject kindObject = new JSONObject();
            kindObject.put("title", kind);

            for (Iterator<?> pit = params.keys(); pit.hasNext(); ) {
                String param = (String) pit.next();
                kindObject.put(param, params.get(param));
            }
            children.put(kindObject);
        }

        JSONObject facility = new JSONObject();
        facility.put("title", facilityName);
        facility.put("children", children);
        destination.put(facility);
    }
    System.out.println(destination.toString(2));
}

关于java - 在 Java 中映射到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10925874/

相关文章:

java - 如何使用java中的socket类设置连接超时?

java - 计时器需要很长时间才能启动

java - 需要帮助在我的 Java GUI 中实现用户输入的监听器

java - jackson 和可怕的 IOException

java - 如何通过java将json数据推送到MONGODB中的数组对象中

java - 网络服务 : error in code or connection

java - 在 UI 线程上获取重复回调的正确方法

javascript - 需要将复杂的json obj转换为合适的angularjs ui树数据json结构

javascript - 字符串到对象的转换?

javascript - 如何循环json字符串