2015-05-19 184 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

相關問題