解析时访问JSON值

时间:2018-08-23 10:51:24

标签: java json parsing jackson fasterxml

我有一个名为persons.json的文件:

[
  {
    "id": 1,
    "name": "The Best",
    "email": "thenextbigthing@gmail.com",
    "birthDate": "1981-11-23"
  },
  {
    "id": 2,
    "name": "Andy Jr.",
    "email": "usa@gmail.com",
    "birthDate": "1982-12-01"
  },
  {
    "id": 3,
    "name": "JohnDoe",
    "email": "gameover@gmail.com",
    "birthDate": "1990-01-02"
  },
  {
    "id": 4,
    "name": "SomeOne",
    "email": "rucksack@gmail.com",
    "birthDate": "1988-01-22"
  },
  {
    "id": 5,
    "name": "Mr. Mxyzptlk",
    "email": "bigman@hotmail.com",
    "birthDate": "1977-08-12"
  }
]

我想用ArrayList将此文件解析为FasterXML,也许可以使用它的ObjectMapper()函数,然后能够访问每个值(id,名称等)遍历新创建的String时,分别作为ArrayList单独使用。我该怎么办?我什至不知道我可以/应该使用哪种类型的列表才能分别访问每个值。我有点卡在这里。 List<???>

4 个答案:

答案 0 :(得分:1)

首先,您应该创建POJO来存储信息:

public class Person {
    Long id;
    String name;
    String email;
    @JsonFormat("yyyy-mm-dd")
    Date birthDate;
    ...
}

接下来您应该致电:

List<Person> myObjects = new ObjectMapper().readValue(jsonInput, new TypeReference<List<Person>>(){});

答案 1 :(得分:1)

首先:

  

FasterXML在下面使用Jackson来解析/产生json。

现在:要使用Jackson,首先要为您的json数据创建一个容器对象,我们将其称为Person

public class Person {
  private int id;
  private String name, email;
  @JsonFormat
  (shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
  private Date birthDate;

  //add here getters and setters ...
}

在这一点上,假设您将pathToPersonsJsonFile作为包含persons.json路径的字符串,则可以这样使用文件:

byte[] jsonData = Files.readAllBytes(Paths.get(pathToPersonsJsonFile));
ObjectMapper objectMapper = new ObjectMapper();
Person[] parsedAsArray = objectMapper.readValue(jsonData, Person[].class); //array
ArrayList<Persons> persons = new ArrayList<>(Arrays.asList(parsedAsArray)); //your list

注意JsonFormat可以声明该值在json中的格式。

答案 2 :(得分:1)

这应该足够了

ObjectMapper objectMapper = new ObjectMapper();
List<Person> list =  objectMapper.readValue(new File("path_to_persons.json"), new TypeReference<List<Person>>(){});

public class Person {
    private String id;
    private String name;
    private String email;
    private String birthDate;
   .....
}

答案 3 :(得分:1)

1)首先创建类Person.java

2)然后读取person.json文件并从中创建一个JSONArray。

3)然后按如下所示进行解析:

class Person{
        private int id;
        private String name;
        private String email;
        private String birthDate;

        public int getId() {
            return id;
        }

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

        public String getName() {
            return name;
        }

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

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getBirthDate() {
            return birthDate;
        }

        public void setBirthDate(String birthDate) {
            this.birthDate = birthDate;
        }
    }


    public List<Person> getPersonList(JSONArray dataArray){
        List<Person> personList = new ArrayList<>();
        for(int i=0; i<dataArray.length(); i++){
            try {
                JSONObject personJsonObject = dataArray.getJSONObject(i);
                Person person = new Person();
                if(personJsonObject.has("id") && !personJsonObject.isNull("id")){
                    person.setId(personJsonObject.getInt("id"));
                }
                if(personJsonObject.has("name") && !personJsonObject.isNull("name")){
                    person.setName(personJsonObject.getString("name"));
                }
                if(personJsonObject.has("email") && !personJsonObject.isNull("email")){
                    person.setEmail(personJsonObject.getString("email"));
                }
                if(personJsonObject.has("birthDate") && !personJsonObject.isNull("birthDate")){
                    person.setBirthDate(personJsonObject.getString("birthDate"));
                }
                personList.add(person);
            }catch (JSONException e){

            }
        }
        return personList;
    }

4)然后在任何地方使用此列表。