2013-04-04 102 views
1

我具有以下類SolrFBLocationDocGSON錯誤:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期BEGIN_OBJECT但STRING位於第1點的列166

public class SolrFBLocationDoc{ 

    @Field 
    private String name; 
    @Field 
    private String id; 
    @Field 
    private Location location = new Location(); 

    //and some more class members 
} 

其中,Location是一類從restfb:com.restfb.types.Location

我想下面給出一個solrDocument轉換爲SolrFBLocationDoc類的一個對象:

SolrFBLocationDoc doc = gson.fromJson(gson.toJson(solrDoc), SolrFBLocationDoc.class); 

其中,solrDoc是:

SolrDocument[{id=106377336067638, location=Location[city=null country=null latitude=null longitude=null state=null street=null zip=null]}] 

gson.toJson(solrDoc)回報,

{"id":"106377336067638","location":"Location[city\u003dnull country\u003dnull latitude\u003dnull longitude\u003dnull state\u003dnull street\u003dnull zip\u003dnull]"} 

但是,它導致了錯誤:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 166 

我可以看到該問題由gson.toJson(solrDoc)由於存在的至Location類對象爲字符串的轉換。

然後沒有使用gson.toJson(solrDoc),我怎樣才能將SolrDocument轉換爲SolrFBLocationDoc

我該如何擺脫這個問題?

回答

0

在你SolrFBLocationDoc類,location伊娃是一種com.restfb.types.Location但到您的JSON字符串:

"location":"Location[city\u003dnull country\u003dnull latitude\u003dnull longitude\u003dnull state\u003dnull street\u003dnull zip\u003dnull]" 

意味着location是一個字符串。由於SolrFBLocationDoc的定義,事實上,在分號後,Gson預計會有一個「{」(即BEGIN_OBJECT)。但它發現"Location..,這是一個字符串,所以它不會解析。

右鍵字符串會是這樣:

{"id":"106377336067638","location":{"city":null, "country":null, "latitude":null, "longitude":null, "state":null, "street":null, "zip":null}} 

因此,這意味着gson.toJson(solrDoc)回報您location鍵轉義字符串。這可能取決於如何定義SolrDocument。可能在該類location字段中是一個字符串。如果你可以把SolrDocument的定義,這個答案可以提煉和假設證實/拒絕。

相關問題