如何使用Gson API为复杂嵌套的json结构提取特定的键值对 [英] How to extract a specific key-value pair for a complex-nested json structure using Gson API

查看:426
本文介绍了如何使用Gson API为复杂嵌套的json结构提取特定的键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最后一个目标是创建一个地图,其中创建了一个创作名称,并提供了从Json下面提取的值(param1和param2的英文值)。例如:{(SW:5000 w),(RW:5800 W)}

  string = 

[
{
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
}
]
}
]
/ pre>

这是我的代码来解压缩它和下面的错误:

  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 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;
}

这篇关于如何使用Gson API为复杂嵌套的json结构提取特定的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆