0

我有一个使用 CouchDB 的 Java 应用程序。为了从中解析 JSON,我使用 lib: com.google.code.svenson v.1.3.8 但是,我遇到了问题。我收到 JSON,并且我想排除其中的一个属性。例子:

{"groups":[], "norms":[], "unit":null, "_attachments":{
    "revision/1-b7c9e44edecb4c6509ce75d84d3aa06d":{
        "content_type":"application/json",
        "revpos":2,
        "digest":"md5-qPtWsDPF7vQANTQvQzmjrA==",
        "length":684,
        "stub":true
    }
}}

我需要排除“_attachments”字段和所有内部。但问题是,属性修订是动态的。它一直在变化。而且我的代码不起作用:

public class TestDTO {

    private String attachment;
    private String revision;
    private String contentType;
    private String revpos;
    private String digest;
    private String length;
    private String stub;

    @JSONProperty(ignore = true, value = "_attachments")
    public void setAttachment(String attachment) {
        this.attachment = attachment;
    }

    public String getRevision() {
        return revision;
    }

    @JSONProperty(ignore = true, value = "revision/")
    public void setRevision(String revision) {
        this.revision = revision;
    }
//other get/set methods 
}
org.svenson.JSONParseException: Cannot set property revision/1-d009693fd000f179495cb3f9087109b6 on class java.lang.String
    at org.svenson.JSONParser.parseObjectInto(JSONParser.java:589)
    at org.svenson.JSONParser.parseObjectInto(JSONParser.java:617)
    at org.svenson.JSONParser.parse(JSONParser.java:396)
    at org.svenson.JSONParser.parse(JSONParser.java:341)
    at com.foo.core.api.services.bar.BarObjectParser.parseToObject(BarObjectParser.java:47)
    at com.foo.core.api.services.bar.task.ChangesProcessorTask.persistObject(ChangesProcessorTask.java:78)
    at com.foo.core.api.services.bar.task.ChangesProcessorTask.processChange(ChangesProcessorTask.java:65)
    at com.foo.core.api.services.bar.task.ChangesProcessorTask.processChanges(ChangesProcessorTask.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
    at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
4

1 回答 1

0

是的,你可以做到。您只需要在业务对象中将地址列表声明为临时属性。

然后将以下代码添加到您的 jsonConfig:

JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreTransientFields(true);
于 2013-04-18T09:46:50.887 回答