如何使用java代码创建以下JSON文件?
{
"mode":"EXTENDED",
"vertices": [
{
"name": {
"type": "string",
"value": "loppper"
},
"lang": {
"type": "string",
"value": "java"
},
"_id": "3",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "vadas"
},
"age": {
"type": "integer",
"value": 27
},
"_id": "2",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "marko"
},
"age": {
"type": "integer",
"value": 29
},
"_id": "1",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "mark12"
},
"age": {
"type": "integer",
"value": 31
},
"_id": "31",
"_type": "vertex"
},
{
"name": {
"type": "string",
"value": "peter"
},
"age": {
"type": "integer",
"value": 35
},
"_id": "6",
"_type": "vertex"
}]}
在JSONObject和JSONArray之间混淆。谁能请帮忙。 我能够创建
{
"mode":"Extended",
"vertices" : [
[(1)
](2)
]
}
但我需要花括号而不是(1)(2)
中的方括号答案 0 :(得分:1)
在Java中,您可以使用gson库生成JSON字符串(并解析它们)。它就像创建一组对象一样简单,保存数据。之后,您拨打Gson.toJson(obj)
。
答案 1 :(得分:0)
我为我编写了一些util类,通过转换Map和List对象来生成JSON
public static String parseMapToJSONString(Map<String,Object> mapData){
StringBuilder jsonData = new StringBuilder("{");
int iteration = 0;
for(String key: mapData.keySet()){
if( mapData.get(key) instanceof List){
jsonData.append("\"").append(key).append("\"").append(":")
.append(parseListToJSONString((List<Object>) mapData.get(key)));
} else if(mapData.get(key) instanceof Map){
jsonData.append("\"").append(key).append("\"").append(":")
.append(parseMapToJSONString((Map<String,Object>) mapData.get(key)));
} else {
jsonData.append("\"").append(key).append("\"").append(":").append("\"").append(mapData.get(key).toString()).append("\"");
}
if(mapData.size() > 1 && iteration < mapData.size() - 1){
jsonData.append(",");
}
iteration++;
}
jsonData.append("}");
return jsonData.toString();
}
public static String parseListToJSONString(List<Object> listData){
StringBuilder jsonListData = new StringBuilder("[");
int iteration = 0;
for(Object value: listData){
if(value instanceof String){
jsonListData.append("\"").append(value.toString()).append("\"");
} else if(value instanceof Map){
jsonListData.append(parseMapToJSONString((Map<String,Object>) value));
}
if(iteration < listData.size()-1){
jsonListData.append(",");
}
iteration++;
}
jsonListData.append("]");
return jsonListData.toString();
}
一些示例代码
Map<String,Object> testMap1 = new HashMap();
testMap1.put("name", "Oscar");
testMap1.put("email","peter@yahoo.com");
List<String> majorList = new ArrayList();
majorList.add("Math");
majorList.add("Physics");
testMap1.put("major",majorList);
Map<String,Object> testMap2 = new HashMap();
testMap2.put("name", "Alice");
testMap2.put("school", "ABC");
testMap1.put("mother",testMap2);
List<Map<String,Object>> friendList = new ArrayList();
Map<String,Object> testMap3 = new HashMap();
testMap3.put("name", "Peter");
testMap3.put("school", "ABC");
Map<String,Object> testMap4 = new HashMap();
testMap4.put("name", "Tom");
testMap4.put("school", "ABC");
friendList.add(testMap3);
friendList.add(testMap4);
testMap1.put("friends",friendList);
System.out.println(JSONParserService.parseMapToJSONString(testMap1));