नेस्टेड मानचित्र के लिए एक समाधान के लिए खोज कर दिया गया समस्या स्वयं और "이종은" ऊपर पहला जवाब था जो वास्तव में गैर तुच्छ उपयोग मामलों में मदद करता था।
के बाद से समाधान केवल संभाला संख्या ऊपर मैं समाधान अद्यतन स्ट्रिंग और भी बूलियन्स के लिए सामान्य पार्स क्षमता प्रदान करने के लिए नीचे दिए गए अद्यतन कोड देखें:
private static class MapDeserializer implements JsonDeserializer<Map<String, Object>> {
public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Map<String, Object> m = new LinkedHashMap<String, Object>();
JsonObject jo = json.getAsJsonObject();
for (Entry<String, JsonElement> mx : jo.entrySet()) {
String key = mx.getKey();
JsonElement v = mx.getValue();
if (v.isJsonArray()) {
m.put(key, g.fromJson(v, List.class));
} else if (v.isJsonPrimitive()) {
Number num = null;
ParsePosition position=new ParsePosition(0);
String vString=v.getAsString();
try {
num = NumberFormat.getInstance(Locale.ROOT).parse(vString,position);
} catch (Exception e) {
}
//Check if the position corresponds to the length of the string
if(position.getErrorIndex() < 0 && vString.length() == position.getIndex()) {
if (num != null) {
m.put(key, num);
continue;
}
}
JsonPrimitive prim = v.getAsJsonPrimitive();
if (prim.isBoolean()) {
m.put(key, prim.getAsBoolean());
} else if (prim.isString()) {
m.put(key, prim.getAsString());
} else {
m.put(key, null);
}
} else if (v.isJsonObject()) {
m.put(key, g.fromJson(v, Map.class));
}
}
return m;
}
}
private static class ListDeserializer implements JsonDeserializer<List<Object>> {
public List<Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
List<Object> m = new ArrayList<Object>();
JsonArray arr = json.getAsJsonArray();
for (JsonElement jsonElement : arr) {
if (jsonElement.isJsonObject()) {
m.add(g.fromJson(jsonElement, Map.class));
} else if (jsonElement.isJsonArray()) {
m.add(g.fromJson(jsonElement, List.class));
} else if (jsonElement.isJsonPrimitive()) {
Number num = null;
try {
num = NumberFormat.getInstance().parse(jsonElement.getAsString());
} catch (Exception e) {
}
if (num != null) {
m.add(num);
continue;
}
JsonPrimitive prim = jsonElement.getAsJsonPrimitive();
if (prim.isBoolean()) {
m.add(prim.getAsBoolean());
} else if (prim.isString()) {
m.add(prim.getAsString());
} else {
m.add(null);
}
}
}
return m;
}
}
private static Gson g = new GsonBuilder().registerTypeAdapter(Map.class, new MapDeserializer()).registerTypeAdapter(List.class, new ListDeserializer()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
स्रोत
2015-08-26 20:04:05
यह भी देखें [यह उत्तर] (http://stackoverflow.com/a/43346123/179864) मैंने इसी तरह के प्रश्न के लिए लिखा था; पकड़ यह है कि आपको डेटा को 'ऑब्जेक्ट' के रूप में पार्स करना होगा और फिर आपको जो चाहिए उसे डालना होगा। – aditsu
जैक्सन नक्शे के प्रकारों का पता लगाने में एक बेहतर काम करता है, और जीएसओएन की तुलना में कॉन्फ़िगर करना कहीं अधिक आसान है: https://github.com/FasterXML/jackson- डाटाबेस –
सवाल जीएसन कार्यान्वयन के लिए स्पष्ट रूप से पूछा गया। कृपया सवाल से चिपके रहें। – Megakoresh