5

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

{
   "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
}

关键字段是动态的,所以我不知道如何编写模型类来读取它。

4

2 回答 2

12

你希望你的模型类看起来如何?

status并且total可能会int,所以只有叶子info

作为一个实验,只需添加一个字段Object info并查看 Gson 将如何将其设置为ArrayList<LinkedHashMap<String, String>>- 丑陋且难以通过键访问,但所有数据都在那里。鉴于这些信息,建模类的最快方法是:

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

如果您可以控制 JSON 的生成方式,我建议将结构info从对象数组更改[{a:b},{c:d},{e:f}]为仅对象{a:b,c:d,e:f}。有了这个,您可以将其映射到 aMap<String, String>并具有所有好处,例如通过密钥访问,keys()并且values()

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

如果您想要后一个模型类而不更改 JSON 格式,则必须编写一个TypeAdapter(或者JsonDeserializer如果您只对解析 JSON 感兴趣,而不是从模型类生成它)。

这是一个 JsonDeserializer 帽子,它将您的原始infoJSON 属性映射到一个普通的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,类似于:

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

请注意,Map<String, String>无论遇到什么类,这都会为任何类注册自定义反序列化器。如果您不想要这个,您还需要创建一个自定义TypeAdapterFactory并在返回和反序列化器实例之前检查声明类。

于 2012-04-22T12:27:37.377 回答
1

这里有一个解决方案,它不需要制作 JsonDeserializer。

您可以创建的只是地图中的 JsonElementMap.Entry<String, JsonElement>并使用 for 循环遍历条目

//parsing string response to json object
JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonString);
//getting root object
JsonObject dateWiseContent = jsonObject.get("rootObject").getAsJsonObject();
    
for (Map.Entry<String, JsonElement> entry : dateWiseContent.entrySet()) {      
    //this gets the dynamic keys
    String  dateKey = entry.getKey();
            
    //you can get any thing now json element,array,object according to json.
            
    JsonArray jsonArrayDates = entry.getValue().getAsJsonArray();
}

在这里阅读更多

于 2017-04-05T12:57:08.593 回答