在gson中解析动态Json对象 [英] Parse Dynamic Json Objects in gson

查看:240
本文介绍了在gson中解析动态Json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的json结构,我如何在Gson的帮助下解析json。我需要将关键值存储在json对象中,我尝试了很多与动态数组相关的例子,但是没有方括号的动态json数组我无法解析使用Gson.Any解决方案还是需要手动Json Parse?如果有人觉得重复评论下面的答案。

I have json structure like this,how I parse the json with help of Gson.I need to store the key values in json object,I tried many example related to dynamic array but dynamic json array without square brace I cannot parse using Gson.Any Solutions or need to do Manual Json Parse?If any one feel duplicate comment the answer below.

{ "Result":[[
      "Title",
      {
        "0": 1323,
        "1": 3358,
        "2": 2123,
        "3": 8536,
        "4": 1399,
        "5": 9303,
        "7": 9732,
        "8": 3433,
        "9": 1383
      }
    ],[
      "Title",
      {
        "0": 1323,
        "1": 3358,
        "2": 2123,
        "3": 8536,
      }
    ]]}


推荐答案

首先,您的JSON会导致抛出异常,因为它无效 - 您在最后一个值末尾有一个额外的逗号e在第二个例子中。 3:8536,应为3:8536

To start, your JSON causes an exception to be thrown because it is invalid - You have an extra comma at the end of the last value in the second example. "3": 8536, should be "3": 8536.

修复后,如果你正确定义对象,这应该是一个简单的任务。以下是我提出的建议:

After fixing that, this should be a simple task provided you define your objects correctly. Here is what I came up with:

public class Results {
    @SerializedName("Result")
    List<Result> results;
}

public class Result {
    String title;
    Map<String, Integer> results;
}

从那里,结果 class需要以特殊方式反序列化,因为 Result 类中的字段不直接映射到JSON中的条目。相反,我们需要拉出每个 Result 中包含的 JsonArray 的第一个和第二个元素,然后解析因此。看起来像这样:

From there, the Result class needs to be deserialized in a special fashion, since the fields in the Result class do not directly map to entries in the JSON. Instead, we need to pull off the first and second elements of the JsonArray that is contained within each Result, and parse it accordingly. That looks like this:

public class ResultDeserializer implements JsonDeserializer<Result> {

    @Override
    public Result deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonArray array = json.getAsJsonArray();

        Result result = new Result();
        result.title = array.get(0).getAsString();

        result.results = new LinkedHashMap<String, Integer>();
        for(Entry<String, JsonElement> entry : array.get(1).getAsJsonObject().entrySet()) {
            result.results.put(entry.getKey(), entry.getValue().getAsInt());
        }

        return result;
    }   
}

请注意,我的示例省略了错误检查。最后,注册这个反序列化器,你应该全部设置:

Note that my example omits error checking. Finally, register this deserializer, and you should be all set:

Gson gson = new GsonBuilder().registerTypeAdapter(Result.class, new ResultDeserializer()).create();

Results results = gson.fromJson(json, Results.class);

for(Result r : results.results) {
    System.out.println("Title = " + r.title);
    for(Entry<String, Integer> entry : r.results.entrySet()) {
        System.out.println("\t " + entry.getKey() + " -> " + entry.getValue());
    }
}

打印:

Title = Title
     0 -> 1323
     1 -> 3358
     2 -> 2123
     3 -> 8536
     4 -> 1399
     5 -> 9303
     7 -> 9732
     8 -> 3433
     9 -> 1383
Title = Title
     0 -> 1323
     1 -> 3358
     2 -> 2123
     3 -> 8536

我会把它留给OP实现反向,这是<$的序列化器c $ c>结果产生相同的结果。

I'll leave it to the OP to implement the reverse, that is a serializer for Result to produce the same results.

这篇关于在gson中解析动态Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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