2015-05-19 186 views
1

我正在试图从JSON反序列化时出现以下错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但BEGIN_OBJECT在行1列1258

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 1258 
+0

您是否检查过JSON以查看它是否有效? 'l_sParamProcessedImage'包含什么?它与“ToggleProcessedImage”的结构相匹配吗?请发布您试图反序列化的JSON,以及'ToggleProcessedImage'的源代码。 –

+0

是的,我发布了l_sParamProcessedImage的值。是的,它匹配toogleProcessedImage的来源 – PSDebugger

+0

不是我不相信你,但它有助于有更多的目光:) - 你可以发布'ToggleProcessedImage'的来源吗? –

回答

3

既然你的避风港没有公布源代码到ToggleProcessedImage(或者它本身可能包含的任何对象),我无法告诉你为什么你的JSON不是反序列化。 Gson需要一个特定字段的数组,但JSON似乎包含该字段的一个对象。

我看着你的JSON列1258(其中错误发生),并认为它是:

"MeasuredBox": { 
    ... 
} 

现在早,你也有:

"MeasuredBoxes": [ 
    ... 
] 

是否有可能在其中一个类别,您意外地将measuredBox字段的类型定义为List<MeasuredBox>MeasuredBox[]而不是仅仅是MeasuredBox?也许你把它与名称相似的字段measuredBoxes混淆了。

编辑

在回答您的评论。您发布的MeasuredBoxes是:

public class MeasuredBoxes { 

    public Box Region; 
    public List<Integer> LayerBottoms; 
    public List<Measurement> Measurements; 
    public List<Box> MeasuredBox; //<--- this is the source of your error 

    ... 
} 

这就是您的错误。类MeasuredBoxes需要属性的Box对象列表。但是,您提供的JSON只有一个Box,它直接表示为一个对象。

为了解决这个问题,您可能需要改变你的JSON这样MeasuredBox是一个数组:

"MeasuredBox": [{ 
    ... 
}] 

或更改MeasuredBoxes类使得MeasuredBoxBox型的,而不是List<Box>

public class MeasuredBoxes { 

    public Box Region; 
    public List<Integer> LayerBottoms; 
    public List<Measurement> Measurements; 
    public Box MeasuredBox; //<--- this is Box now instead of List<Box> 

    ... 
} 

另一方面,请使用Java命名约定。变量(这包括班级字段)和方法应该是namedLikeThis(即骆驼式)和NotLikeThis,但是班级应该是NamedLikeThis

最好保留班级成员private;使他们public是例外,而不是规则。

+0

在MasuredBoxes中我有一个Region,LayerBottom和一个MeasuredBox列表,所以我没有感到困惑。将有一个MeasuredBox列表 – PSDebugger

+0

@PSDebugger请发布源'ToggleProcessedImage'及其所使用的任何内部对象。否则,我们只是在玩猜谜游戏。 –

+0

public Box Region; //必需 公开列表 LayerBottoms; //必需 公开名单测量; // required public List MeasuredBox; //必需 – PSDebugger

相关问题