如何使用Gson API

时间:2016-04-26 20:29:48

标签: java json dictionary gson

我的最终目标是创建一个带有创作名称的地图和下面Json的提取值(param1和param2的英文值)。例如:{(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
            }
        ]
    }
]

以下是我提取它的代码及其下面的错误:

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

错误:

Index: 2, Size: 1

我正在使用的结构有些东西,我使用了GSon API。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

第一行是抛出错误的那一行:

JsonArray jarray = (JsonArray) jarray1.get(2);

jarray1只包含一个元素(您的根对象,具有属性titlesortspecValues的元素),因此您获得的错误是有意义的。

尝试这样的事情:

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

答案 1 :(得分:0)

    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();

阅读文档JsonArray get方法继承自java.util.List,因此第一个元素是索引0.试试这个并告诉我!