Java 将JSON解析为映射
我试图将下面的JSON字符串解析为键值对,以便将其插入数据库中的表中Java 将JSON解析为映射,java,json,gson,Java,Json,Gson,我试图将下面的JSON字符串解析为键值对,以便将其插入数据库中的表中 { "name": "MyMobile", "category": "cellphone", "details": { "displayAspectRatio": "97:3", "audioConnector": "none", "motherBoard": { "Rom": "256GB", "Ram":
{
"name": "MyMobile",
"category": "cellphone",
"details": {
"displayAspectRatio": "97:3",
"audioConnector": "none",
"motherBoard": {
"Rom": "256GB",
"Ram": "8GB",
"Battery": "400mAH"
}
}
}
我能够使用以下代码和GSON
解析JSON字符串,并将其存储到映射中。但不幸的是,它对嵌套的JSON对象不起作用
public static HashMap<String, Object> createHashMapFromJsonString(String json) {
JsonObject object = (JsonObject) parser.parse(json);
Set<Map.Entry<String, JsonElement>> set = object.entrySet();
Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
HashMap<String, Object> map = new HashMap<String, Object>();
while (iterator.hasNext()) {
Map.Entry<String, JsonElement> entry = iterator.next();
String key = entry.getKey();
JsonElement value = entry.getValue();
if (null != value) {
if (!value.isJsonPrimitive()) {
if (value.isJsonObject()) {
map.put(key, createHashMapFromJsonString(value.toString()));
} else if (value.isJsonArray() && value.toString().contains(":")) {
List<HashMap<String, Object>> list = new ArrayList<>();
JsonArray array = value.getAsJsonArray();
if (null != array) {
for (JsonElement element : array) {
list.add(createHashMapFromJsonString(element.toString()));
}
map.put(key, list);
}
} else if (value.isJsonArray() && !value.toString().contains(":")) {
map.put(key, value.getAsJsonArray());
}
} else {
map.put(key, value.getAsString());
}
}
}
return map;
}
}
对于嵌套JSON,上述问题可以通过递归解决。请参阅下面的代码。注意:仅处理映射、//TODO ArrayList和异常处理
@Test
public void testJsonToMap() {
String data = "{\"name\":\"MyMobile\",\"category\":\"cellphone\",\"details\":{\"displayAspectRatio\":\"97:3\",\"audioConnector\":\"none\",\"motherBoard\":{\"Rom\":\"256GB\",\"Ram\":\"8GB\",\"Battery\":\"400mAH\"}}}";
Gson gson = new GsonBuilder().create();
Map jsonMap = gson.fromJson(data, Map.class);
System.out.println(jsonMap);
Sep2019JavaTest test = new Sep2019JavaTest();
Map opMap = test.toMap(jsonMap, new HashMap<String, Object>(), "");
System.out.println(opMap);
}
private Map toMap(Map ipMap, Map opMap, String parentKey) {
if (ipMap != null && !ipMap.isEmpty()) {
ipMap.forEach((k, v) -> {
String key = parentKey.isEmpty() ? k.toString() : parentKey.concat(".").concat(k.toString());
if (v.getClass() == LinkedTreeMap.class) {
toMap((Map) v, opMap, key);
} else {
opMap.put(key, v);
}
});
}
return opMap;
}
//Output
//{details.motherBoard.Rom=256GB, details.motherBoard.Battery=400mAH, name=MyMobile, category=cellphone, details.audioConnector=none, details.motherBoard.Ram=8GB, details.displayAspectRatio=97:3}
@测试
public void testJsonToMap(){
字符串数据=“{\'name\”:“MyMobile\”,“category\”:“Phone\”,“details\”:{\'displayAspectRatio\”:“97:3\”,“audioConnector\”:“none\”,“motherBoard\”:{\'Rom\”:“256GB\”,“Ram\”:“8GB\”,“Battery\”:“400mAH\”};
Gson Gson=new GsonBuilder().create();
Map jsonMap=gson.fromJson(数据,Map.class);
System.out.println(jsonMap);
Sep2019JavaTest=新的Sep2019JavaTest();
Map opMap=test.toMap(jsonMap,newhashmap(),“”);
系统输出打印项次(opMap);
}
私有映射toMap(映射ipMap、映射opMap、字符串parentKey){
if(ipMap!=null&&!ipMap.isEmpty()){
ipMap.forEach((k,v)->{
字符串key=parentKey.isEmpty()?k.toString():parentKey.concat(“.”).concat(k.toString());
if(v.getClass()==LinkedTreeMap.class){
toMap((Map)v,opMap,key);
}否则{
opMap.put(键,v);
}
});
}
返回opMap;
}
//输出
//{details.motherBoard.Rom=256GB,details.motherBoard.Battery=400mAH,name=MyMobile,category=phone,details.audioConnector=none,details.motherBoard.Ram=8GB,details.displayAspectRatio=97:3}
@Test
public void testJsonToMap() {
String data = "{\"name\":\"MyMobile\",\"category\":\"cellphone\",\"details\":{\"displayAspectRatio\":\"97:3\",\"audioConnector\":\"none\",\"motherBoard\":{\"Rom\":\"256GB\",\"Ram\":\"8GB\",\"Battery\":\"400mAH\"}}}";
Gson gson = new GsonBuilder().create();
Map jsonMap = gson.fromJson(data, Map.class);
System.out.println(jsonMap);
Sep2019JavaTest test = new Sep2019JavaTest();
Map opMap = test.toMap(jsonMap, new HashMap<String, Object>(), "");
System.out.println(opMap);
}
private Map toMap(Map ipMap, Map opMap, String parentKey) {
if (ipMap != null && !ipMap.isEmpty()) {
ipMap.forEach((k, v) -> {
String key = parentKey.isEmpty() ? k.toString() : parentKey.concat(".").concat(k.toString());
if (v.getClass() == LinkedTreeMap.class) {
toMap((Map) v, opMap, key);
} else {
opMap.put(key, v);
}
});
}
return opMap;
}
//Output
//{details.motherBoard.Rom=256GB, details.motherBoard.Battery=400mAH, name=MyMobile, category=cellphone, details.audioConnector=none, details.motherBoard.Ram=8GB, details.displayAspectRatio=97:3}