尝试使用jackson streaming api解析json

时间:2016-02-11 19:26:50

标签: java json jackson

我有这个json

[{
  "id":"1",
  "firstname":"X",
  "lastname":"y",
  "contactnumbers":["905123123"]
},{
  "id":"2",
  "firstname":"Jo",
  "lastname":"Be",
  "contactnumbers":["416888888", "951233333"]
}]

我正在尝试使用以下代码解析它

    if (jsonParser.nextToken() == JsonToken.START_ARRAY){
        if (jsonParser.nextToken() == JsonToken.START_OBJECT){
            c = new Person();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT){
                if (jsonParser.getCurrentName().equals("id")){
                    c.setId(jsonParser.getValueAsString());
                    jsonParser.nextToken();
                } else if (jsonParser.getCurrentName().equals("firstname")){
                    c.setFirstName(jsonParser.getValueAsString());
                    jsonParser.nextToken();
                } else if (jsonParser.getCurrentName().equals("lastname")){
                    c.setLastName(jsonParser.getValueAsString());
                    jsonParser.nextToken();
                } else if (jsonParser.getCurrentName().equals("contactnumbers")){
                    List<String> s = new ArrayList<String>();
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY){
                        s.add(jsonParser.nextValue().asString());
                    }
                }
            }
        }
        if (c != null) {
            personList.add(c);
        }
    }

但显然,即使是我最外面的if也没有被执行。我不知道为什么。

1 个答案:

答案 0 :(得分:0)

代码不是很好,但它确实有用。

Person.java:

package org.jackson.example;

import java.sql.Date;
import java.util.List;

public class Person {

    private String id;

    private String firstName;

    private String lastName;

    private List<String> contactNumbers;

    public String getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<String> getContactNumbers() {
        return contactNumbers;
    }

    public void setContactNumbers(List<String> contactNumbers) {
        this.contactNumbers = contactNumbers;
    }

    @Override
    public String toString() {
        return "Person [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", contactNumbers="
                + contactNumbers + "]";
    }

}

StreamJacksonApi.java:

package org.jackson.example;

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

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class StreamJacksonApi {

    public static void main(String[] args) throws JsonParseException, IOException {
        List<Person> personList = new ArrayList<>();

        String content = "[{ \"id\":\"1\", \"firstname\":\"X\", \"lastname\":\"y\", \"contactnumbers\":[\"905123123\"] },{ \"id\":\"2\", \"firstname\":\"Jo\", \"lastname\":\"Be\", \"contactnumbers\":[\"416888888\", \"951233333\"] }]"; 
        JsonFactory jsonFactory = new JsonFactory();
        @SuppressWarnings("deprecation")
        JsonParser jsonParser = jsonFactory.createJsonParser(content); //Omit deprecation
        jsonParser.nextToken();
        while (jsonParser.nextToken() != JsonToken.END_ARRAY){
                final Person c = new Person();
                while (jsonParser.nextToken() != JsonToken.END_OBJECT){
                    if (jsonParser.getCurrentName().equals("id")){
                        c.setId(jsonParser.getValueAsString());
                    } else if (jsonParser.getCurrentName().equals("firstname")){
                        c.setFirstName(jsonParser.getValueAsString());
                    } else if (jsonParser.getCurrentName().equals("lastname")){
                        c.setLastName(jsonParser.getValueAsString());
                    } else if (jsonParser.getCurrentName().equals("contactnumbers")){
                        List<String> s = new ArrayList<String>();
                        while (jsonParser.nextToken() != JsonToken.END_ARRAY){
                            final String token = jsonParser.getValueAsString();
                            if(token != null) {
                                s.add(token);
                            }
                        }
                        c.setContactNumbers(s);
                    }
                }
                if (c != null) {
                    personList.add(c);
                }
            }

        for (Person person : personList) {
            System.out.println(person);
        }
    }


}

另请参阅JacksonStreamingApi