如何从json中删除Scope和declaredType.I我正在使用jackson解析器

时间:2015-09-14 09:02:03

标签: java json spring jackson

这是我的输出

{\"dealReleaseReference\":[{\"name\":\"DealReleaseReference\",\"declaredType\":\"java.lang.Object\",\"scope\":\"net.ddex.xml._2011.ern_main._33.ReleaseDeal\",\"value\":{\"name\":\"ReleaseReference\",\"declaredType\":\"java.lang.String\",\"scope\":\"net.ddex.xml._2011.ern_main._33.Release\",\"value\":\"Rs9973241\",\"nil\":false,\"globalScope\":false,\"typeSubstituted\":false},\"nil\":false,\"globalScope\":false,\"typeSubstituted\":true}],\"deal\":[{\"dealTerms\":{\"commercialModelType\":[{\"value\":\"PAY_AS_YOU_GO_MODEL\"}],\"usage\":[{\"useType\":[{\"value\":\"PERMANENT_DOWNLOAD\"},{\"value\":\"ON_DEMAND_STREAM\"}]}],\"territoryCode\":[\"Worldwide\"],\"priceInformation\":[{\"priceRangeType\":{\"value\":\"SINGLE3\",\"namespace\":\"PADPIDA2010051102D\"}}],\"validityPeriod\":[{\"startDate\":{\"value\":1435343400000}}]}},{\"dealTerms\":{\"commercialModelType\":[{\"value\":\"SUBSCRIPTION_MODEL\"}],\"usage\":[{\"useType\":[{\"value\":\"CONDITIONAL_DOWNLOAD\"},{\"value\":\"ON_DEMAND_STREAM\"}]}],\"territoryCode\":[\"Worldwide\"],\"priceInformation\":[{\"priceRangeType\":{\"value\":\"SINGLE3\",\"namespace\":\"PADPIDA2010051102D\"}}],\"validityPeriod\":[{\"startDate\":{\"value\":1435343400000}}]}},{\"dealTerms\":{\"commercialModelType\":[{\"value\":\"ADVERTISEMENT_SUPPORTED_MODEL\"}],\"usage\":[{\"useType\":[{\"value\":\"CONDITIONAL_DOWNLOAD\"},{\"value\":\"ON_DEMAND_STREAM\"}]}],\"territoryCode\":[\"Worldwide\"],\"priceInformation\":[{\"priceRangeType\":{\"value\":\"SINGLE3\",\"namespace\":\"PADPIDA2010051102D\"}}],\"validityPeriod\":[{\"startDate\":{\"value\":1435343400000}}]}}],\"effectiveDate\":1438108200000}

我想从json中删除Scope并声明类型

这是我的班级

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.08.07 at 04:23:43 PM IST 
//


package net.ddex.xml._2011.ern_main._33;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.datatype.XMLGregorianCalendar;


/**
 * A Composite containing details of one or more Deals pertaining to one
 *                 or more Releases.
 * 
 * <p>Java class for ReleaseDeal complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="ReleaseDeal">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="DealReleaseReference" type="{http://ddex.net/xml/20110630/ddex}LocalReleaseAnchorReference" maxOccurs="unbounded"/>
 *         &lt;element name="Deal" type="{http://ddex.net/xml/2011/ern-main/33}Deal" maxOccurs="unbounded"/>
 *         &lt;element name="EffectiveDate" type="{http://www.w3.org/2001/XMLSchema}date"/>
 *       &lt;/sequence>
 *       &lt;attribute name="LanguageAndScriptCode" type="{http://www.w3.org/2001/XMLSchema}string" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ReleaseDeal", propOrder = {
    "dealReleaseReference",
    "deal",
    "effectiveDate"
})
public class ReleaseDeal  {

    @XmlElementRef(name = "DealReleaseReference", type = JAXBElement.class)
    protected List<JAXBElement<Object>> dealReleaseReference;
    @XmlElement(name = "Deal", required = true)
    protected List<Deal> deal;
    @XmlElement(name = "EffectiveDate", required = true)
    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar effectiveDate;
    @XmlAttribute(name = "LanguageAndScriptCode")
    protected String languageAndScriptCode;

    /**
     * Gets the value of the dealReleaseReference property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the dealReleaseReference property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getDealReleaseReference().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link Object }{@code >}
     * 
     * 
     */
    public List<JAXBElement<Object>> getDealReleaseReference() {
        if (dealReleaseReference == null) {
            dealReleaseReference = new ArrayList<JAXBElement<Object>>();
        }
        return this.dealReleaseReference;
    }

    /**
     * Gets the value of the deal property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the deal property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getDeal().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link Deal }
     * 
     * 
     */
    public List<Deal> getDeal() {
        if (deal == null) {
            deal = new ArrayList<Deal>();
        }
        return this.deal;
    }

    /**
     * Gets the value of the effectiveDate property.
     * 
     * @return
     *     possible object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public XMLGregorianCalendar getEffectiveDate() {
        return effectiveDate;
    }

    /**
     * Sets the value of the effectiveDate property.
     * 
     * @param value
     *     allowed object is
     *     {@link XMLGregorianCalendar }
     *     
     */
    public void setEffectiveDate(XMLGregorianCalendar value) {
        this.effectiveDate = value;
    }

    /**
     * Gets the value of the languageAndScriptCode property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getLanguageAndScriptCode() {
        return languageAndScriptCode;
    }

    /**
     * Sets the value of the languageAndScriptCode property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setLanguageAndScriptCode(String value) {
        this.languageAndScriptCode = value;
    }

}

这是我用来将类转换为json

的步骤
ObjectMapper objectMapper = new ObjectMapper();
                        objectMapper.setSerializationInclusion(Include.NON_NULL);
                        objectMapper.setSerializationInclusion(Include.NON_EMPTY);
                //      objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
                        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
                        objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
                        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

                        String releaseDealSTring="";
                        try {
                            releaseDealSTring = objectMapper.writeValueAsString(releaseDeal);

                        } catch (JsonProcessingException e) {
                            // TODO Auto-generated catch block

                            System.out.print(e.getStackTrace());
                        }

感谢您的帮助.Jack就是这种情况。如何删除。我已经尝试过gson但是失败了.Jackson正确地解析了它但是添加了范围和声明类型。

1 个答案:

答案 0 :(得分:0)

JAXBElement看起来像实例变量declaredTypescope。注释它们以忽略值将阻止它们被序列化。

例如,使用jackson使用@JsonIgnore注释(在JAXBElement类内):

@JsonIgnore
private String declaredType;
@JsonIgnore
private String scope;