简体   繁体   中英

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). eg: {(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. 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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