jcouchdb: svenson unable to parse json string
I'm trying to use jcouchdb (https://code.google.com/p/jcouchdb/) for accessing my CouchDB instance from Java. I have some JSon documents that I'd like to parse into Java classes - with Svenson, used in jcouchd开发者_StackOverflow中文版b, and then put those parsed objects into DB. I generate this JSON objects with AVRO (http://avro.apache.org) JSon Encoder, they seem to be ok, but apparently other parsers have problems with them.
My JSon strings look like this:
{
"id":40,
"event_id":"48764322212",
"note":{
"string":"ABC note"
},
"created_date":null,
"event_category":null,
"city":null,
"address":null
}
Which seems valid JSON - validated with http://jsonformatter.curiousconcept.com/
However my Svenson object defined like this:
public class Note {
Long id;
String eventId;
String note;
String createdDate;
String eventCategory;
String city;
String address;
@JSONProperty()
public Long getId() {
@JSONProperty("event_id")
public String getEventId() {
@JSONProperty("note")
public String getNote() {
@JSONProperty("created_date")
public String getCreatedDate() {
@JSONProperty("event_category")
public String getEventCategory() {
@JSONProperty("city")
public String getCity() {
@JSONProperty("address")
public String getAddress() {
}
(setters and getters' bodies intentionally removed)
The error when parsing is:
Cannot set property string on class java.lang.String
It seems that this JSON is parsed correctly (there is a difference in note field):
{
"id":40,
"event_case_id":"000-123123123",
"event_msisdn":"48764322212",
"note":"Planowana data portacji: 2011/01/27 11:42:49",
"created_date":null,
"event_category":null,
"city":null,
"address":null
}
How can I work this out? Perhaps there is another json library that would work for me?
You declare note as a java.lang.String:
public String getNote()
but in the JSON you declare it as an Object with a property named "string":
"note":{
"string":"ABC note"
}
You need to change the JSON or the Bean to match each other. For example, in the second functioning JSON, you declared the JSON note as a string. This is why it works.
精彩评论