我需要解析这种JSON:
{
"commodities": {
"39": "GOLD",
"41": "SILVER",
"42": "PLATINUM-APR16",
"85": "SUGAR (11) ",
"108": "WHEAT",
"116": "OIL-MAR16 (WTI CRUDE)",
"130": "CORN ",
"158": "COFFEE ",
"180": "ORANGE S.A.",
"282": "GOLD/JPY",
"304": "GOLD/EUR",
"332": "GOLD/TRY",
"468": "CRB INDEX",
"508": "COPPER",
...and a LOT more...
},
"currencies": {
"2": "USD/JPY",
"35": "AUD/USD",
"38": "USD/ILS",
...and a LOT more...
},
如何将此JSON保存到Map
?所以我可以这样使用它:
String value = mapCommodities.get(key);
String value = mapCommodities.get(39) //value equals "GOLD"
问题是我不知道如何将这个索引标记从JSON解析为整数值。我认为需要编写自定义Deserealizer
,但实际上并不知道如何。
答案 0 :(得分:2)
创建自定义反序列化器
public class CustomDeserializer implements JsonDeserializer<List<Map<Integer, String>>>{
@Override
public List<Map<Integer, String>> deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
List<Map<Integer, String>> randomList = new ArrayList<>();
JsonObject parentJsonObject = element.getAsJsonObject();
Map<Integer, String> childMap;
for(Map.Entry<String, JsonElement> entry : parentJsonObject.entrySet()){
childMap = new HashMap<>();
for(Map.Entry<String, JsonElement> entry1 : entry.getValue().getAsJsonObject().entrySet()){
childMap.put(Integer.parseInt(entry1.getKey()), entry1.getValue().toString());
}
randomList.add(childMap);
}
return randomList;
}
}
通过
使用它GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(new TypeToken<ArrayList<Map<Integer, String>>>() {}.getType(), new CityListDeserializer());
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
List<Map<Integer, String>> randomList = gson.fromJson(String.valueOf(object), new TypeToken<ArrayList<Map<Integer, String>>>() {}.getType());
你可以通过
使用它randomList.get(index).get(39);
如果你想要它Map<Map<Integer, String>>
,也可以这样做。也会更新。但我不建议对于非常大的数据集。 HashMaps将消耗大量内存
修改强>
你也可以这样做
public class CityListDeserializer implements JsonDeserializer<Map<String, Map<Integer, String>>>{
@Override
public Map<String, Map<Integer, String>> deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Map<String, Map<Integer, String>> randomList = new HashMap<>();
JsonObject parentJsonObject = element.getAsJsonObject();
Map<Integer, String> childMap;
for(Map.Entry<String, JsonElement> entry : parentJsonObject.entrySet()){
childMap = new HashMap<>();
for(Map.Entry<String, JsonElement> entry1 : entry.getValue().getAsJsonObject().entrySet()){
childMap.put(Integer.parseInt(entry1.getKey()), entry1.getValue().toString());
}
randomList.put(entry.getKey(), childMap);
}
return randomList;
}
}
使用它
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(new TypeToken<Map<String, Map<Integer, String>>>() {}.getType(), new CityListDeserializer());
Gson gson = builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
Map<String, Map<Integer, String>> randomList = gson.fromJson(String.valueOf(object), new TypeToken<Map<String, Map<Integer, String>>>() {}.getType());
通过
访问该值randomList.get("commodities").get(39);
这将返回给你GOLD
这一切都适用于普通的json解析。不确定,但我想只是像我给的那样给出类型代码将使其适用于Retrofit
答案 1 :(得分:1)
这是你可以做的:)
首先使用
将响应转换为JSONARRAYJSONArray jsonArray = new JSONArray("your string");
然后您可以迭代或因为您知道respobnse的结构,您可以像以下一样访问它:)
JSON commodityJSON = jsonArray.getJSONObject(0);
JSON currencies = jsonArray.getJSONObject(1);
一旦你让JSON对象使用
访问它commodityJSON.getString("39");
commodityJSON.getString("41");
修改强>
根据你的评论:)你可以做这样的事我相信:)
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Iterator<String> objectKeys = jsonObject.keys();
for( String s : yourKeys){
System.out.println(jsonObject.getString(s));
}
}
它会帮助伙伴:)快乐的编码伙伴:)