3 回答
TA贡献1883条经验 获得超3个赞
这是您可以GSON用来反序列化 JSON 的方法:
public static void main(String[] args) {
final String json = "{\n" +
" \"object\": {\n" +
" \"1\": [{ \"id\" : 111 }],\n" +
" \"2\": [{ \"id\" : 222 }],\n" +
" \"3\": [{ \"id\" : 333 }]\n" +
" }\n" +
"}\n";
final Gson gson = new GsonBuilder()
.create();
final ObjectWrapper value = gson.fromJson(json, ObjectWrapper.class);
System.out.println(value.object);
System.out.println(value.object.keySet());
System.out.println(value.object.get(1));
}
// This is top-most object we want to deserialize from JSON
static class ObjectWrapper {
// Didn't bother about proper naming while it is better to give a meaningful name here
private Map<Integer, List<Element>> object;
}
static class Element {
// Added this attribute to demonstrate that objects within array are properly read
private int id;
@Override
public String toString() {
return "{id=" + id + "}";
}
}
TA贡献1744条经验 获得超4个赞
我现在使用以下解决方法:
json.replace("\"1\"","\"one\"")
.replace("\"2\"","\"two\"")
.replace("\"3\"","\"three\"")
.replace("\"4\"","\"four\"")
.replace("\"5\"","\"five\"")
.replace("\"6\"","\"six\"")
.replace("\"7\"","\"seven\"");
添加回答
举报