解析时访问 JSON 值



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

我想使用FasterXML将这个文件解析为一个ArrayList,可能使用它ObjectMapper()函数,然后能够在迭代新创建的ArrayList时作为String单独访问每个值(id、名称等(。我该怎么做?我什至不知道我可以/应该使用哪种列表来单独访问每个值。我有点被困在这里了。List<???>

首先:

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 上的格式。

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

这应该足够了

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( 首先创建类人.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(然后在任何您想要的地方使用此列表。

相关内容

  • 没有找到相关文章

最新更新