如何通过使用GSON android的分析与动态“钥匙”的JSON [英] How to parse a JSON with dynamic “key” in android by using GSON

查看:140
本文介绍了如何通过使用GSON android的分析与动态“钥匙”的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用GSON库来解析所有的JSON字符串,并得到一个JSON对象。 但现在我需要分析是这样的:

I have been using GSON library to parse all the json string and get a JSON object. But now I need to parse is like this:

{
   "status":1,
   "info":[
      {
         "\u5a31\u4e50":"\u51b7\u76d8,\u9ad8\u811a\u676f,\u6211\u7684\u7cd6\u679c\u5c4b,\u670d\u52a1\u4e1a\u6d88\u8d39\u52b5"
      },
      {
         "\u7f8e\u5986":"\u4e2a\u62a4|\u5316\u5986#\u9762\u90e8\u62a4\u7406,\u4e2a\u4eba\u536b\u751f,\u8eab\u4f53\u62a4\u7406,\u9999\u6c34\u9999\u6c1b,\u6c90\u6d74|\u7f8e\u53d1\u7528\u54c1,\u5f69\u5986,\u7cbe\u6cb9SPA,\u773c\u90e8\u62a4\u7406,\u78e8\u7802\u53bb"
      },
      {
         "\u8863\u670d":"\u670d|\u9970|\u978b|\u5e3d#\u670d\u88c5,\u978b\u9774,\u5185\u8863,\u914d\u9970,\u536b\u8863,\u4f11\u95f2\u88e4,T\u6064,\u88d9\u5b50,\u886c\u886b,\u9488\u7ec7\u886b,\u5a74\u5e7c\u513f\u670d\u9970"
      }
   ],
   "total":3
}

关键领域是动态的,所以我不知道怎么写一个模型类阅读本。

The key fields are dynamic, so I don't know how to write a model class to read this.

推荐答案

你会怎样想你的模型类看?

How would you like your model class to look?

状态很可能是 INT ,因此,只有叶信息

status and total would probably be int, so that only leaves info.

作为一个实验,只需添加一个字段对象信息,看看GSON将其设置为的ArrayList< LinkedHashMap的<字符串,字符串>&GT ; - 丑陋不堪,通过密钥访问,但所有的数据是存在的。鉴于信息,一类模型的最快方法是:

As an experiment, just add a field Object info and see how Gson would set it to an ArrayList<LinkedHashMap<String, String>> -- ugly and hard to access by key, but all the data is there. Given that information, the fastest way to model a class would be:

class Something {
  int status;
  List<Map<String, String> info;
  int total;
}

如果你有超过JSON是如何产生的,我建议从对象的数组 [{变化的信息的结构控制:乙},{C:D} {E:F}] 来只是一个对象 {A:B,C:D,E:F} 。有了这个,你可以只映射到一个地图&LT;字符串,字符串&GT; 中包含通过密钥访问的所有优点,键()值()

If you have control over how that JSON is generated, I suggest changing the structure of info from an array of objects [{a:b},{c:d},{e:f}] to just an object {a:b,c:d,e:f}. With this, you could just map it to a Map<String, String> with all the benefits like access by key, keys() and values():

class Something {
  int status;
  Map<String, String> info;
  int total;
}

如果你想在不改变JSON格式,后者模型类,你必须写一个 TypeAdapter (或 JsonDeserializer 如果你只关心解析JSON,而不是从你的模型类生成的话)。

If you want the latter model class without changing the JSON format, you'll have to write a TypeAdapter (or JsonDeserializer if you're only interested in parsing JSON, not generating it from your model class).

下面是一个JsonDeserializer帽子将映射原始的信息 JSON属性设置为一个普通的地图&LT;字符串,字符串&GT;

Here's a JsonDeserializer hat would map your original info JSON property to a plain Map<String, String>.

class ArrayOfObjectsToMapDeserializer
    implements JsonDeserializer<Map<String, String>> {

  public Map<String, String> deserialize(JsonElement json, Type typeOfT,
      JsonDeserializationContext context) throws JsonParseException {
    Map<String, String> result = new HashMap<String, String>();

    JsonArray array = json.getAsJsonArray();
    for (JsonElement element : array) {
      JsonObject object = element.getAsJsonObject();
      // This does not check if the objects only have one property, so JSON
      // like [{a:b,c:d}{e:f}] will become a Map like {a:b,c:d,e:f} as well.
      for (Entry<String, JsonElement> entry : object.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue().getAsString();
        result.put(key, value);
      }
    }
    return result;
  }
}

您需要注册这个定制 JsonDeserializer 与此类似:

You need to register this custom JsonDeserializer similar to this:

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(
    new TypeToken<Map<String, String>>() {}.getType(),
    new ArrayOfObjectsToMapDeserializer());
Gson gson = builder.create();

请注意,该注册自定义解串器的任何地图&LT;字符串,字符串&GT; 无论在什么类是遇到。如果你不想这样,你需要创建一个自定义 TypeAdapterFactory ,以及与解串器的返回和实例之前检查声明类。

Note that this registers the custom deserializer for any Map<String, String> regardless in what class it is encountered. If you don't want this, you'll need to create a custom TypeAdapterFactory as well and check the declaring class before returning and instance of the deserializer.

这篇关于如何通过使用GSON android的分析与动态“钥匙”的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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