2012-09-05 94 views
1

我正面臨有關GSON json與Java的問題。我在這裏查找了很多帖子,但我找不到解決方案。所以我在這裏列出我的問題。非常感謝 !GSON:java.lang.IllegalStateException:期望的BEGIN_OBJECT,但是STRING

這裏是我的JSON數據

"{ 
    "data": { 
     "marks": "", 
     "spines": "", 
     "dendrites": { 
      "voxel": [ 
       [383,382,382,381], 
       [49,50,51,52], 
       [7,10,10,10], 
       [0,0,0,0] 
      ], 
      "maxint": [32,42,28,28], 
      "maxintcorrected": null, 
      "maxintcube": null, 
      "medianfiltered": [54.36979745,54.36979745,54.36979745,54.36979745], 
      "meanbacksingle": null, 
      "maxintsingle": null, 
      "thres": null, 
      "meanback": 42, 
      "index": 1, 
      "length": [0,0.3223757885,0.6336712814,0.9350672197,1.227262867], 
      "XYlength": [0,0.2085982964,0.410997367,0.6084466603] 
     } 
    } 
} 
" 

我的班級的認定中是繼

public class Test { 
    public data data; 

    public class data { 

     public Object marks; 
     public String spines = ""; 
     public StandardSpinenalysisImage.data.dendrites[] dendrites; 

     public class dendrites { 

      public int voxel[][]; 
      public int maxint[]; 
      public String maxintcorrected = ""; 
      public String maxintcube = ""; 
      public int medianfiltered[]; 
      public String meanbacksingle = ""; 
      public String maxintsingle = ""; 
      public int thres = 0; 
      public int meanback; 
      public int index = 0; 
      public int length[]; 
      public int XYlength[]; 

      public dendrites() { 
       this.thres = 100; 
      } 
     } 
    } 
} 

但是當我使用的代碼 測試T = g.fromJson(輸入的Test.class);

我得到了錯誤結果。

Exception in thread "AWT-EventQueue-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5 

非常感謝你非常非常!

+0

有「標記」的類型是「對象」的原因其他類型是「String」? –

+0

爲什麼在開始和結束你的json文件時有'''? – giorashc

+0

@JasonSperske我只是測試它...我認爲標記也是這裏的一種字符串,只需測試 – Lawrence

回答

2

你的json看起來不錯。只是嘗試刪除「從一開始就和JSON結束

你可以嘗試使用this link驗證您的JSON

+0

Hi ,這個類怎麼樣?這個類是否在這裏?謝謝:) – Lawrence

+0

JSOn很好。「用於告訴它的一個字符串.. String json =」that above json「; –

1

今天面對同樣的錯誤。在我這裏,問題是在JSON一個變量映射到一個字符串,但在類變量是在類類型的對象,這讓GSON認爲JSON變量應該是類的類型,導致IllegalStateException異常與Expected BEGIN_OBJECT but was STRING

希望它可以幫助別人。

相關問題