我试图使用jcouchdb(https://code.google.com/p/jcouchdb/)从Java访问我的CouchDB实例。我有一些JSon文档需要解析到Java类中 - 使用jcouchdb中的Svenson,然后将这些解析的对象放入数据库中。我用AVRO(http://avro.apache.org)JSon Encoder生成这个JSON对象,它们似乎没问题,但显然其他解析器有问题。jcouchdb:svenson无法解析json字符串
我的JSON字符串是这样的:
{
"id":40,
"event_id":"48764322212",
"note":{
"string":"ABC note"
},
"created_date":null,
"event_category":null,
"city":null,
"address":null
}
这似乎有效的JSON - 验证与http://jsonformatter.curiousconcept.com/
但是我的史云逊对象定义是这样的:
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() {
}
(getter和setter方法'身体故意删除)
错误解析时是:
Cannot set property string on class java.lang.String
看来,这个JSON是正确解析(存在注领域有所作为):
{
"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
}
我如何来解决这一问题?也许有另一个json库可以为我工作?
好吧,这似乎很明显+错误消息。你能否建议我应该如何定义我的bean,以便上面的JSON字符串可以工作?我无法修改Avro生成的json。 – 2011-03-22 08:01:10
我现在无法测试它,但我认为您应该使用[type selection](http://code.google.com/p/svenson/wiki/TypeSelection)将带有“string”属性的对象映射到String 。 – 2011-03-22 08:34:21
好吧,添加一个简单的:parser.addTypeHint(“。string”,String.class)没有帮助 - 我应该使用不同的匹配语法吗? – 2011-03-22 10:26:16