【发布时间】:2013-11-29 11:59:54
【问题描述】:
我的班级:
public class MyClass {
public HashMap<Integer, HashMap<String, PointFMP[] >> matchingPages;
public static class PointFMP{
public float x;
public float y;
}
}
我的 Json:
{
"matchingPages": {
"1": {
"Butter": [
{
"x": 16.23,
"y": 21.11
},
{
"x": 18.18,
"y": 26.67
}
],
"Cake": [
{
"x": 13.23,
"y": 21.11
}
]
},
"2": {
"Other value": [
{
"x": 41.98,
"y": 47.62
}
]
}
}
}
解析:
Gson gson = new GsonBuilder().create();
MyClass response = gson.fromJson(jsonString, MyClass.class);
我的错误:
11-29 12:56:47.017: W/System.err(8169): com.google.gson.JsonSyntaxException:java.lang.IllegalStateException: 应为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING 11-29 12:56:47.022:W/System.err(8169):在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
知道如何正确解析它吗?
【问题讨论】:
-
BEGIN_OBJECT 但是是 STRING 意味着,当 gson 反序列化您的 json 时,matchingPages 键“1”是字符串而不是 int/integer,所以 gson 说它的“1”、“2”键是字符串不是整数对象。
-
我不这么认为,因为改变 public HashMap
> matchingPages;到 public HashMap > matchingPages;没有帮助,我在其他地方用公共 Map 项目解析;以下json“项目”:{“146226”:{ -
看到我已经回复了 PointFMP 和 Butter 类,你的 json 格式是正确的。哦,我认为您没有看到 json 中的匹配页面是字符串。