解析时访问 JSON 个值

Accessing JSON values when parsing

我有一个名为 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"
  }
]

我想将此文件解析为 ArrayListFasterXML,可能还有 ObjectMapper() 函数,然后能够访问每个值(id、名称等) .) 在遍历新创建的 ArrayList 时单独作为 String。我怎么能那样做?我什至不知道我 could/should 使用哪种列表来单独访问每个值。我有点被困在这里。 List<???>

首先您应该创建用于存储信息的 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>>(){});

首先:

FasterXML uses Jackson underneath to parse/produce 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.

上的格式

这应该够了

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;
   .....
}

1)先创建classPerson.java

2) 然后读取 persons.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) 然后随心所欲地使用这个列表。