我需要在java中构建json并且不知道如何,因为它有很多括号。
[{
"id": "52350f7bbc39dd030800007e",
"quantity": 2
},{
"id": "52352e7ebc39ddc8ef0000c7",
"quantity": 2,
"selections": [{
"id": "52352e7ebc39ddc8ef0000c8",
"item_ids":[
"52352e7ebc39ddc8ef0000c9",
"52352e7ebc39ddc8ef0000ca"
]
}]
}]
答案 0 :(得分:2)
创建两个类,如下所示:
Raw struture:
Class JsonEx
{
String id;
String quantity;
List<Selection> selection;
}
Class Selection
{
String id;
List<String> item_ids;
}
将属性设置为JsonEx
类,包括Selection
,然后使用GSON创建json,如下所示:
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(JsonEx obj);