I have following class SolrFBLocationDoc
:
public class SolrFBLocationDoc{
@Field
private String name;
@Field
private String id;
@Field
private Location location = new Location();
//and some more class members
}
where, Location
is a class from restfb: com.restfb.types.Location
.
I'm trying to convert a solrDocument
to an object of class SolrFBLocationDoc
as given below:
SolrFBLocationDoc doc = gson.fromJson(gson.toJson(solrDoc), SolrFBLocationDoc.class);
where, solrDoc
is:
SolrDocument[{id=106377336067638, location=Location[city=null country=null latitude=null longitude=null state=null street=null zip=null]}]
and gson.toJson(solrDoc)
returns,
{"id":"106377336067638","location":"Location[city\u003dnull country\u003dnull latitude\u003dnull longitude\u003dnull state\u003dnull street\u003dnull zip\u003dnull]"}
But, it's resulting into the error:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 166
I can see the problem is occuring due to conversion of Location
class object to String by gson.toJson(solrDoc)
.
Then without using gson.toJson(solrDoc)
, how can I convert SolrDocument
to SolrFBLocationDoc
?
How can I get rid of this problem?
In your SolrFBLocationDoc
class, location
ivar is of kind com.restfb.types.Location
but into your JSON string:
"location":"Location[city\u003dnull country\u003dnull latitude\u003dnull longitude\u003dnull state\u003dnull street\u003dnull zip\u003dnull]"
means that location
is a string. Due to SolrFBLocationDoc
definition, indeed, after semicolon, Gson expects a "{" (that is a BEGIN_OBJECT
). But it finds "Location..
, that is a string, so it won't parse.
Right string would be like:
{"id":"106377336067638","location":{"city":null, "country":null, "latitude":null, "longitude":null, "state":null, "street":null, "zip":null}}
So this means that gson.toJson(solrDoc)
returns you a escaped string for location
key. This could depends on how SolrDocument
is defined. Probably in that class location
field is a string. If you could put definition of SolrDocument
, this answer can be refined and hypothesis confirmed/rejected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.