映射到Java中的JSON [英] Map to JSON in Java

查看:106
本文介绍了映射到Java中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的地图 地图:

i have a map like this 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格式

I want to convert it into JSON formated like this

[

    {"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,但是结果却不是我想要的:

I tried to use GSON But the resulting output was not what I wanted:

{
  "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格式?

how can a get a json formated as i want??

推荐答案

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

You need to transform your JSON to the new format you have provided.

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

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

Using GSON

Create some classes that you want to handle the data, they can look something like:

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;
    }
}

下一步也是查看源代码,并对其进行表述.我会使用:

The next step is too look at the source and create a represenatation of it. I would use:

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

因为输入数据像这样布置.现在使用Gson进行转换非常简单:

since the input data is layed out like it. To convert it using Gson now is quite easy:

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中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆