如何使用JAXB解析递归JSON对象

时间:2016-04-26 21:08:31

标签: json parsing jaxb

下面是我的JSON,如何使用jaxb解析它,注意类别的深度和关键点会改变

{
   "categories":{
      "categoryHierarchy":[
         {
            "hierarchy":{
               "SDG--1513417390":{
                  "SD-1501363638":{
                     "D-HS-MM4":{
                        "CL-HS-MM4-DEFAULT":{
                           "C-HS-MM4-MM0203":{

                           }
                        }
                     }
                  }
               }
            },
            "precedence":1
         }
      ]
   }
}

1 个答案:

答案 0 :(得分:0)

After trying multiple things I was able to parse the recursive category structure.

    package com.example;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pearson.aem.aemcore.pim.dto.com.example.Example;

class Parser{

    private transient static ObjectMapper jsonMapper;

    public static void main (String [] args) throws IOException
    {
        jsonMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        String response = "{\"categoryHierarchy\":[{\"hierarchy\":{\"SDG--1513417390\":{\"SD-1501363638\":{\"D-HS-MM4\":{\"CL-HS-MM4-DEFAULT\":{\"C-HS-MM4-MM0203\":{}}}}}},\"precedence\":1}]}";
        InputStream stream = new ByteArrayInputStream(response.getBytes());

        Example ex = unmarshalJson(stream, Example.class);

        System.out.println(ex.getCategoryHierarchy().get(0).getHierarchy1().getAdditionalProperties());
        //System.out.println(ex.getCategoryHierarchy().get(0).getHierarchy().getAdditionalProperties().get("SDG--1513417390").keySet.iterator().next());
    }


    public static <T> T unmarshalJson(final InputStream result, final Class<T> declaredType) throws IOException
    {
        return (T) jsonMapper.readValue(result, declaredType);
    }

}

----------------
package com.example;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlElement;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.pearson.aem.aemcore.pim.dto.CategoryElementDTO;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Example
{
    @XmlElement(
            name = "categoryHierarchy")
    private List<CategoryHierarchy> categoryHierarchy;

    @XmlElement(
            name = "relatedCategories")
    private Map<String, CategoryElementDTO> relatedCategories;

    public List<CategoryHierarchy> getCategoryHierarchy()
    {
        return categoryHierarchy;
    }

    public void setCategoryHierarchy(List<CategoryHierarchy> categoryHierarchy)
    {
        this.categoryHierarchy = categoryHierarchy;
    }

    public Map<String, CategoryElementDTO> getRelatedCategories()
    {
        return relatedCategories;
    }

    public void setRelatedCategories(Map<String, CategoryElementDTO> relatedCategories)
    {
        this.relatedCategories = relatedCategories;
    }
}
------------
package com.example;

public class CategoryHierarchy
{
    private Hierarchy hierarchy;
    private Integer precedence;

    public Hierarchy getHierarchy()
    {
        return hierarchy;
    }
    public void setHierarchy(Hierarchy hierarchy)
    {
        this.hierarchy = hierarchy;
    }
    public Integer getPrecedence()
    {
        return precedence;
    }
    public void setPrecedence(Integer precedence)
    {
        this.precedence = precedence;
    }

}

---------------
package com.example;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Hierarchy
{
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties()
    {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value)
    {
        this.additionalProperties.put(name, value);
    }
}