jcouchdb:svenson 无法解析 json 字符串
我正在尝试使用 jcouchdb (https://code.google.com/p/jcouchdb/) 从 Java 访问我的 CouchDB 实例。我有一些 JSon 文档,我想使用 Svenson 解析成 Java 类,在 jcouchdb 中使用,然后将这些解析的对象放入 DB 中。我用 AVRO (http://avro.apache.org) Json 编码器生成这个 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/ 进行验证
然而我的Svenson对象是这样定义的:
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和getters的主体被故意删除)
解析时的错误是:
Cannot set property string on class java.lang.String
看来这个JSON解析正确(有一个差异在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
}
我该如何解决这个问题?也许还有另一个 json 库适合我?
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 jcouchdb, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将 note 声明为 java.lang.String:
但在 JSON 中,您将其声明为具有属性 named“string”的对象:
您需要更改 JSON 或 Bean 以相互匹配。例如,在第二个起作用的 JSON 中,您将 JSON 注释声明为字符串。这就是它起作用的原因。
You declare note as a java.lang.String:
but in the JSON you declare it as an Object with a property named "string":
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.