简体   繁体   English

如何使用杰克逊流式API解析给定的json?

[英]How do I parse the given json using jackson streaming api?

Input json (sample taken for 3 employees, however there can be many employees). 输入json(样本为3名员工,但是可以有很多员工)。 I have used http://www.jsonschema2pojo.org/ to get the pojo classes. 我已经使用http://www.jsonschema2pojo.org/来获取pojo类。

I am able to parse all the elements except the contact array for the sample employees. 我能够解析示例员工的除联系人数组之外的所有元素。 How can I parse the contact array having 2 nodes in the jackson parser method? 如何在杰克逊解析器方法中解析具有2个节点的联系人数组?

JSON - JSON-

[

{
    "id": 123,
      "name": "Pankaj",
      "permanent": true,
      "address": {
        "street": "Albany Dr",
        "city": "San Jose",
        "zipcode": 95129
       },
    "phoneNumbers": [
        123456,
        987654
    ],
      "role": "Manager",
      "cities": [
        "Los Angeles",
        "New York"
    ],
       "contact" : [
       { "type" : "phone/home", "ref" : "333-333-1234"},
       { "type" : "phone/work", "ref" : "444-444-4444"}
    ]

},
{
      "id": 234,
      "name": "Test",
      "permanent": true,
      "address": {
        "street": "BBSR Dr",
        "city": "San Jose",
        "zipcode": 4556
       },
      "phoneNumbers": [
        545614,
        54622
      ],
      "role": "SSE",
      "cities": [
        "Los Angeles",
        "New York"
       ],
      "contact" : [
      { "type" : "phone/home", "ref" : "333-333-1234"},
      { "type" : "phone/work", "ref" : "444-444-4444"}
      ]
},

{
      "id": 1231,
      "name": "Test123",
      "permanent": true,
      "address": {
            "street": "BBSR Dr",
            "city": "Bhubaneswar",
            "zipcode": 4556
      },
      "phoneNumbers": [
            545614,
            54622
       ],
      "role": "SSE",
      "cities": [
        "Los Angeles",
        "New York",
        "Bhubaneswar"
       ]
     "contact" : [
     { "type" : "phone/home", "ref" : "333-333-1234"},
     { "type" : "phone/work", "ref" : "444-444-4444"}
     ]
}
]

Employee.java Employee.java

public class Employee {

private Integer id;
private String name;
private Boolean permanent;
private Address address;
private long[] phoneNumbers;
private String role;
private List<String> cities = null;
private List<Contact> contact = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
.
.
.

Contact.java Contact.java

public class Contact {

private String type;
private String ref;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
.
.
.

Jackson Parser method() 杰克逊解析器方法()

private static void parseJSON(JsonParser jsonParser, Employee emp,
        List<Long> phoneNums) throws JsonParseException, IOException {

    //loop through the JsonTokens
    while(jsonParser.nextToken() != JsonToken.END_OBJECT){
        String name = jsonParser.getCurrentName();
        if("id".equals(name)){
            jsonParser.nextToken();
            emp.setId(jsonParser.getIntValue());
        }else if("name".equals(name)){
            jsonParser.nextToken();
            emp.setName(jsonParser.getText());
        }else if("permanent".equals(name)){
            jsonParser.nextToken();
            emp.setPermanent(jsonParser.getBooleanValue());
        }else if("address".equals(name)){
            jsonParser.nextToken();
            //nested object, recursive call
            parseJSON(jsonParser, emp, phoneNums);
        }else if("street".equals(name)){
            jsonParser.nextToken();
            emp.getAddress().setStreet(jsonParser.getText());
        }else if("city".equals(name)){
            jsonParser.nextToken();
            emp.getAddress().setCity(jsonParser.getText());
        }else if("zipcode".equals(name)){
            jsonParser.nextToken();
            emp.getAddress().setZipcode(jsonParser.getIntValue());
        }else if("phoneNumbers".equals(name)){
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                phoneNums.add(jsonParser.getLongValue());
            }
        }else if("role".equals(name)){
            jsonParser.nextToken();
            emp.setRole(jsonParser.getText());
        }else if("cities".equals(name)){
            jsonParser.nextToken();
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                emp.getCities().add(jsonParser.getText());

            }
        }else if ("contact".equals(name)){
                            jsonParser.nextToken();
                            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                                    emp.get
                            }
                    }

    }
}

First of all you have one error in your json structure. 首先,您的json结构有一个错误。 The last employee has the value of 最后一位雇员的价值为

"cities": [
        "Los Angeles",
        "New York",
        "Bhubaneswar"
] // here you missed a comma
"contact" : [
     { "type" : "phone/home", "ref" : "333-333-1234"},
     { "type" : "phone/work", "ref" : "444-444-4444"}
]

The correct one should be like : 正确的应该是这样的:

"cities": [
        "Los Angeles",
        "New York",
        "Bhubaneswar"
],
"contact" : [
     { "type" : "phone/home", "ref" : "333-333-1234"},
     { "type" : "phone/work", "ref" : "444-444-4444"}
]

And you can deserialize from json to java object with jackson 2 as below. 您可以使用jackson 2从json反序列化为Java对象,如下所示。 You don't need to parse manually. 您不需要手动解析。 It parses correctly. 它可以正确解析。

public class Main {

    public static void main(String[]args) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        List<Employee> employees = mapper.readValue(new File("test.json"), List.class);
    }
}


class Address {

    private String street;
    private String city;
    private Integer zipcode;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Integer getZipcode() {
        return zipcode;
    }

    public void setZipcode(Integer zipcode) {
        this.zipcode = zipcode;
    }

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

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

}

class Contact {

    private String type;
    private String ref;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

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

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

}

class Employee {

    private Integer id;
    private String name;
    private Boolean permanent;
    private Address address;
    private List<Integer> phoneNumbers = null;
    private String role;
    private List<String> cities = null;
    private List<Contact> contact = null;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getPermanent() {
        return permanent;
    }

    public void setPermanent(Boolean permanent) {
        this.permanent = permanent;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<Integer> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<Integer> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<String> getCities() {
        return cities;
    }

    public void setCities(List<String> cities) {
        this.cities = cities;
    }

    public List<Contact> getContact() {
        return contact;
    }

    public void setContact(List<Contact> contact) {
        this.contact = contact;
    }

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

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM