如何使用Gson API为复杂嵌套的json结构提取特定的键值对 [英] How to extract a specific key-value pair for a complex-nested json structure using Gson API
问题描述
string =
/ pre>
[
{
title:规格,
sort:1,
specValues:[
{
name :xxx,
type:yyy,
englishValue:xx.0 lb,
metricValue:yy.0 kg,
sort:4
},
{
name:Param1,
type:measurement,
englishValue 5000.0 W,
metricValue:5000.0 W,
sort:99
},
{
name:Param2,
type:measurement,
englishValue:5800.0 W,
metricValue:5800.0 W,
sort:99
},
{
name:Frequency,
type:measurement,
englishValue:60.0 Hz,
metricValue:60.0 Hz,
sort:99
}
]
}
]
这是我的代码来解压缩它和下面的错误:
public Map< String,String> parseGenGson(String jsonLine){
Map< String,String> generatorMap = new LinkedHashMap< String,String>();
try {
JsonElement jelement = new JsonParser()。parse(jsonLine);
JsonArray jarray1 = jelement.getAsJsonArray();
JsonArray jarray =(JsonArray)jarray1.get(2);
JsonObject jobject1 = jarray.getAsJsonObject();
JsonArray jarray2 = jobject1.getAsJsonArray(specValue);
JsonObject jobject = jarray2.get(1).getAsJsonObject();
String result1 = jobject.get(englishValue)。toString();
generatorMap.put(Running Watts,result1);
jobject = jarray.get(2).getAsJsonObject();
String result2 = jobject.get(englishValue)。toString();
generatorMap.put(Starting Watts,result2);
log.info(生成器映射+ generatorMap);
return generatorMap;
} catch(Exception e){
log.error(e.getMessage());
return generatorMap;
}
}
错误:
索引:2,大小:1
我使用的结构是关闭的,我使用了GSon API。任何想法?
解决方案第一行是抛出错误的一个:
JsonArray jarray =(JsonArray)jarray1.get(2);
jarray1
只包含一个元素对象,属性为title
,sort
,specValues
)所以你得到的错误是有道理的。
尝试这样的东西:
Map< String,String> generatorMap = new LinkedHashMap< String,String>();
try {
JsonElement jelement = new JsonParser()。parse(jsonLine);
JsonArray jarray1 = jelement.getAsJsonArray();
JsonObject rootObject =(JsonObject)jarray1.get(0);
JsonArray specValues = rootObject.getAsJsonArray(specValues);
JsonObject first =(JsonObject)specValues.get(0);
JsonObject second =(JsonObject)specValues.get(1);
String result1 = first.get(englishValue)。toString();
generatorMap.put(Running Watts,result1);
String result2 = second.get(englishValue)。toString();
generatorMap.put(Starting Watts,result2);
return generatorMap;
} catch(Exception e){
log.error(e.getMessage());
return generatorMap;
}
My final aim is to have a map with the a authored name and the extracted value from the Json below (english-value for param1 and param2). e.g: {(S-W: 5000 w),(R-W: 5800 W)}
string = [ { "title":"Specifications", "sort":1, "specValues":[ { "name":"xxx", "type":"yyy", "englishValue":"xx.0 lb", "metricValue":"yy.0 kg", "sort":4 }, { "name":"Param1", "type":"measurement", "englishValue":"5000.0 W", "metricValue":"5000.0 W", "sort":99 }, { "name":"Param2", "type":"measurement", "englishValue":"5800.0 W", "metricValue":"5800.0 W", "sort":99 }, { "name":"Frequency", "type":"measurement", "englishValue":"60.0 Hz", "metricValue":"60.0 Hz", "sort":99 } ] } ]
Here is my code to extract it and the errors below it:
public Map<String, String> parseGenGson(String jsonLine) { Map <String,String> generatorMap = new LinkedHashMap <String,String>(); try { JsonElement jelement = new JsonParser().parse(jsonLine); JsonArray jarray1 = jelement.getAsJsonArray(); JsonArray jarray = (JsonArray) jarray1.get(2); JsonObject jobject1 = jarray.getAsJsonObject(); JsonArray jarray2 = jobject1.getAsJsonArray("specValue"); JsonObject jobject = jarray2.get(1).getAsJsonObject(); String result1 = jobject.get("englishValue").toString(); generatorMap.put("Running Watts",result1); jobject = jarray.get(2).getAsJsonObject(); String result2 = jobject.get("englishValue").toString(); generatorMap.put("Starting Watts",result2); log.info("Map of generators" + generatorMap); return generatorMap; } catch (Exception e){ log.error(e.getMessage()); return generatorMap; } }
Error:
Index: 2, Size: 1
Something is off with the structure I am using, I used GSon API. Any ideas?
解决方案The first line is the one that is throwing the error:
JsonArray jarray = (JsonArray) jarray1.get(2);
jarray1
contains just one element (your root object, the one with propertiestitle
,sort
,specValues
) so the error you get makes sense.Try something like this:
Map<String, String> generatorMap = new LinkedHashMap<String, String>(); try { JsonElement jelement = new JsonParser().parse(jsonLine); JsonArray jarray1 = jelement.getAsJsonArray(); JsonObject rootObject = (JsonObject) jarray1.get(0); JsonArray specValues = rootObject.getAsJsonArray("specValues"); JsonObject first = (JsonObject) specValues.get(0); JsonObject second = (JsonObject) specValues.get(1); String result1 = first.get("englishValue").toString(); generatorMap.put("Running Watts", result1); String result2 = second.get("englishValue").toString(); generatorMap.put("Starting Watts", result2); return generatorMap; } catch (Exception e) { log.error(e.getMessage()); return generatorMap; }
这篇关于如何使用Gson API为复杂嵌套的json结构提取特定的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!