简体   繁体   English

如何使用Gson API为复杂嵌套的json结构提取特定的键值对

[英]How to extract a specific key-value pair for a complex-nested json structure using Gson API

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). 我的最终目标是创建一个地图,其中包含一个创作的名称,以及从下面的Json中提取的值(param1和param2的英语值)。 eg: {(SW: 5000 w),(RW: 5800 W)} 例如:{(SW:5000 w),(RW: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. 我使用的结构与某些东西不符,我使用了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 properties title , sort , specValues ) so the error you get makes sense. jarray1仅包含一个元素(您的根对象,具有属性titlesortspecValues ),因此您得到的错误是有道理的。

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;
}
    JsonArray  jarray1 = jelement.getAsJsonArray();
    JsonArray jarray = (JsonArray) jarray1.get(0);
    JsonObject jobject1 = jarray.getAsJsonObject();
    JsonArray jarray2 = jobject1.getAsJsonArray("specValue");
    JsonObject jobject = jarray2.get(0).getAsJsonObject();
    String result1 = jobject.get("englishValue").toString();
    generatorMap.put("Running Watts",result1);
    jobject = jarray.get(1).getAsJsonObject();

Reading the docs JsonArray get method is inherited from java.util.List so the first element is the index 0. Try this and let me know! 阅读docs JsonArray get方法是从java.util.List继承的,因此第一个元素是索引0。尝试一下,让我知道!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM