java param=json字符串,映射到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}

}

}

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

]

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

}

}

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

解决方案

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

Data to convert:

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

Using GSON

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

static class Facility {

List children = new LinkedList();

}

static class Kind {

String title;

Map params;

public Kind(String title, Map 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>>

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>>>() {}.getType();

Map>> source =

gson.fromJson(json, type);

Map dest = new HashMap();

for (String facilityName : source.keySet()) {

Map> 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));

}

Using 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));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值