英文:
selection of values by keys of json file using jackson
问题
以下是完成任务的步骤:
步骤 1: 导入所需的Jackson库
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
步骤 2: 创建数据类 Data
public class Data {
private int id;
private String firstName;
private String lastName;
private String dateOfBirth;
private String city;
// 构造函数、Getter和Setter方法等...
}
步骤 3: 在主类中读取JSON文件并解析数据
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
List<Data> data = Arrays.asList(mapper.readValue(Paths.get("C:\\data.json").toFile(), Data[].class));
// 输出解析后的数据
System.out.println(data);
} catch (JsonParseException jsonParseException) {
jsonParseException.printStackTrace();
} catch (JsonMappingException jsonMappingException) {
jsonMappingException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
步骤 4: 进行数据处理以满足任务要求
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
List<Data> data = Arrays.asList(mapper.readValue(Paths.get("C:\\data.json").toFile(), Data[].class));
// 1. 获取年龄在20到30之间的人,按姓名排序
List<Data> peopleBetween20And30 = data.stream()
.filter(person -> person.getAge() >= 20 && person.getAge() <= 30)
.sorted(Comparator.comparing(Data::getFirstName))
.collect(Collectors.toList());
// 2. 获取唯一的城市列表
HashSet<String> uniqueCities = data.stream()
.map(Data::getCity)
.collect(Collectors.toCollection(HashSet::new));
// 3. 获取不同年龄区间的人数
Map<String, Long> ageGroupCounts = data.stream()
.collect(Collectors.groupingBy(Data::getAgeGroup, Collectors.counting()));
// 输出结果
System.out.println("People between 20 and 30: " + peopleBetween20And30);
System.out.println("Unique cities: " + uniqueCities);
System.out.println("Age group counts: " + ageGroupCounts);
} catch (JsonParseException jsonParseException) {
jsonParseException.printStackTrace();
} catch (JsonMappingException jsonMappingException) {
jsonMappingException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
请注意,上述代码假定 Data
类具有名为 getAge()
和 getAgeGroup()
的方法,用于计算年龄和年龄区间。你需要在 Data
类中添加这些方法,以及其他必要的辅助方法。
以上就是完成任务的基本步骤和代码。希望这对你有所帮助!如果有任何问题,请随时问我。
英文:
This is possibly the dumbest question on the entire site. I am new to Java and JSON and I need help. I am using API Jackson. The program receives a JSON file. From it I need to get:
- List of people between the ages of 20 and 30, sorted by name
- Unique list of cities;
- The number of people with an age interval of 0-10, 11-20, 21-30 and so on.
At the moment I have learned how to translate a json file into a List java
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
List<Data> data = Arrays.asList(mapper.readValue(Paths.get("C:\\data.json").toFile(), Data[].class));
System.out.println(data);
} catch (JsonParseException jsonParseException) {
jsonParseException.printStackTrace();
} catch (JsonMappingException jsonMappingException) {
jsonMappingException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
A class was created to read the json file
public class Data {
private int id;
private String firstName;
private String lastName;
private String dateOfBirth;
private String city;
public Data(int id, String firstName, String lastName, String dateOfBirth, String city) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
this.city = city;
}
public Data() {
}
public int getId() {
return id;
}
public void setId(int 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 String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "[id = " + id + ", firstName = " + firstName + ", lastName = " + lastName + ", dateOfBirth = " + dateOfBirth + ", city = " + city + "]";
}
}
Json file looks like this
[
{
"id": "1",
"firstName": "Lesley",
"lastName": "Bryan",
"dateOfBirth": "11/28/61",
"city": "Southampton–Portsmouth"
},
{
"id": "2",
"firstName": "Edward",
"lastName": "Houston",
"dateOfBirth": "10/5/92",
"city": "Southampton–Portsmouth"
},
{
"id": "3",
"firstName": "Donald",
"lastName": "Ross",
"dateOfBirth": "12/10/79",
"city": "Glasgow"
},
{
"id": "4",
"firstName": "Peter",
"lastName": "Kelly",
"dateOfBirth": "3/17/04",
"city": "Birmingham–Wolverhampton"
},
{
"id": "5",
"firstName": "Anthony",
"lastName": "McKinney",
"dateOfBirth": "3/6/68",
"city": "Liverpool"
},
{
"id": "6",
"firstName": "David",
"lastName": "Stewart",
"dateOfBirth": "4/11/73",
"city": "Leeds–Bradford"
},
{
"id": "7",
"firstName": "Christopher",
"lastName": "Austin",
"dateOfBirth": "12/28/74",
"city": "Birmingham–Wolverhampton"
},
{
"id": "8",
"firstName": "Alvin",
"lastName": "Hodge",
"dateOfBirth": "11/25/58",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "9",
"firstName": "Gerald",
"lastName": "Higgins",
"dateOfBirth": "6/28/55",
"city": "Liverpool"
},
{
"id": "10",
"firstName": "Amos",
"lastName": "Owens",
"dateOfBirth": "1/16/01",
"city": "Manchester-Salford"
},
{
"id": "11",
"firstName": "Christian",
"lastName": "Bishop",
"dateOfBirth": "11/14/50",
"city": "Nottingham"
},
{
"id": "12",
"firstName": "Robert",
"lastName": "Caldwell",
"dateOfBirth": "12/8/80",
"city": "Manchester-Salford"
},
{
"id": "13",
"firstName": "Brian",
"lastName": "Heath",
"dateOfBirth": "9/23/02",
"city": "Newcastle upon Tyne–Sunderland"
},
{
"id": "14",
"firstName": "Mark",
"lastName": "Anthony",
"dateOfBirth": "1/8/92",
"city": "London"
},
{
"id": "15",
"firstName": "Mark",
"lastName": "Watson",
"dateOfBirth": "7/27/91",
"city": "Nottingham"
},
{
"id": "16",
"firstName": "Charles",
"lastName": "Stafford",
"dateOfBirth": "1/26/90",
"city": "Birmingham–Wolverhampton"
},
{
"id": "17",
"firstName": "Steven",
"lastName": "Merritt",
"dateOfBirth": "12/4/63",
"city": "Leeds–Bradford"
},
{
"id": "18",
"firstName": "John",
"lastName": "Holmes",
"dateOfBirth": "4/22/52",
"city": "Southampton–Portsmouth"
},
{
"id": "19",
"firstName": "Mervin",
"lastName": "Lewis",
"dateOfBirth": "10/27/95",
"city": "Birmingham–Wolverhampton"
},
{
"id": "20",
"firstName": "Peter",
"lastName": "Marsh",
"dateOfBirth": "12/10/63",
"city": "Glasgow"
},
{
"id": "21",
"firstName": "Piers",
"lastName": "Harrington",
"dateOfBirth": "4/27/85",
"city": "London"
},
{
"id": "22",
"firstName": "Matthew",
"lastName": "O’Brien’",
"dateOfBirth": "1/19/59",
"city": "Manchester-Salford"
}
]
Please tell me in what sequence of actions to complete the task. Once again I apologize for such a stupid message.
P.S.: All names, surnames and dates from the json file were obtained at random.
答案1
得分: 0
首先,您需要更新 Data
类以便处理年龄和年龄组:
- 将
dateOfBirth
的类型更改为LocalDate
,更新构造函数/获取器/提供自定义的设置器以处理 20 世纪的日期 - 添加方法以获取年龄(以年为单位)和年龄组(以字符串形式)
- 更新
toString
方法以打印年龄
class Data {
// ...
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "M/d/yy")
private LocalDate dateOfBirth;
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dob) {
if (dob.isAfter(LocalDate.now())) {
dob = dob.minusYears(100);
}
this.dateOfBirth = dob;
}
@JsonIgnore
public int getAge() {
return Period.between(dateOfBirth, LocalDate.now()).getYears();
}
@JsonIgnore
public String getAgeGroup() {
int age = getAge();
if (age < 11) {
return "0..10";
}
return (age/10*10+1) + ".." + ((age/10 + 1) * 10);
}
@Override
public String toString() {
return "[id = " + id + ", age=" + getAge() + ", firstName = " + firstName + ", lastName = " + lastName + ", dateOfBirth = " + dateOfBirth + ", city = " + city + "]";
}
}
然后,您可以从输入列表中获取所需的数据子集:
// 获取年龄在 20 到 30 岁之间的人,按姓氏/名字排序
List<Data> age20to30 = data
.stream()
.filter(p -> p.getAge() >= 20 && p.getAge() < 30)
.sorted(Comparator.comparing(p -> p.getLastName() + " " + p.getFirstName()))
.collect(Collectors.toList());
age20to30.forEach(System.out::println);
// 获取唯一城市的排序列表(使用 TreeSet 作为 SortedSet)
Set<String> cities = data
.stream()
.map(Data::getCity)
.collect(Collectors.toCollection(TreeSet::new));
cities.forEach(System.out::println);
// 按年龄组获取统计信息
Map<String, Long> byAges = data
.stream()
.collect(Collectors.groupingBy(Data::getAgeGroup, TreeMap::new, Collectors.counting()));
System.out.println(byAges);
输出
[id = 14, age=28, firstName = Mark, lastName = Anthony, dateOfBirth = 1992-01-08, city = London]
[id = 2, age=27, firstName = Edward, lastName = Houston, dateOfBirth = 1992-10-05, city = Southampton–Portsmouth]
[id = 19, age=24, firstName = Mervin, lastName = Lewis, dateOfBirth = 1995-10-27, city = Birmingham–Wolverhampton]
[id = 15, age=29, firstName = Mark, lastName = Watson, dateOfBirth = 1991-07-27, city = Nottingham]
Birmingham–Wolverhampton
Glasgow
Leeds–Bradford
Liverpool
London
Manchester-Salford
Newcastle upon Tyne–Sunderland
Nottingham
Southampton–Portsmouth
{11..20=3, 21..30=4, 31..40=3, 41..50=3, 51..60=4, 61..70=5}
英文:
First, you need to update Data
class to facilitate handling of ages and age groups:
- Change type of
dateOfBirth
toLocalDate
, update constructor/getter/provide custom setter to handle dates in the 20-th century - Add methods to get age in years and age group as String:
- Update
toString
to print age
class Data {
// ...
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "M/d/yy")
private LocalDate dateOfBirth;
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dob) {
if (dob.isAfter(LocalDate.now())) {
dob = dob.minusYears(100);
}
this.dateOfBirth = dob;
}
@JsonIgnore
public int getAge() {
return Period.between(dateOfBirth, LocalDate.now()).getYears();
}
@JsonIgnore
public String getAgeGroup() {
int age = getAge();
if (age < 11) {
return "0..10";
}
return (age/10*10+1) + ".." + ((age/10 + 1) * 10);
}
@Override
public String toString() {
return "[id = " + id + ", age=" + getAge() + ", firstName = " + firstName + ", lastName = " + lastName + ", dateOfBirth = " + dateOfBirth + ", city = " + city + "]";
}
}
Then you can get required subsets of data from the input list:
// get people aged from 20 to 30 sorted by last name/first name
List<Data> age20to30 = data
.stream()
.filter(p -> p.getAge() >= 20 && p.getAge() < 30)
.sorted(Comparator.comparing(p -> p.getLastName() + " " + p.getFirstName()))
.collect(Collectors.toList());
age20to30.forEach(System.out::println);
// get sorted list of unique cities (using TreeSet as a SortedSet)
Set<String> cities = data
.stream()
.map(Data::getCity)
.collect(Collectors.toCollection(TreeSet::new));
cities.forEach(System.out::println);
// get stats by age groups
Map<String, Long> byAges = data
.stream()
.collect(Collectors.groupingBy(Data::getAgeGroup, TreeMap::new, Collectors.counting()));
System.out.println(byAges);
Output
[id = 14, age=28, firstName = Mark, lastName = Anthony, dateOfBirth = 1992-01-08, city = London]
[id = 2, age=27, firstName = Edward, lastName = Houston, dateOfBirth = 1992-10-05, city = Southampton–Portsmouth]
[id = 19, age=24, firstName = Mervin, lastName = Lewis, dateOfBirth = 1995-10-27, city = Birmingham–Wolverhampton]
[id = 15, age=29, firstName = Mark, lastName = Watson, dateOfBirth = 1991-07-27, city = Nottingham]
Birmingham–Wolverhampton
Glasgow
Leeds–Bradford
Liverpool
London
Manchester-Salford
Newcastle upon Tyne–Sunderland
Nottingham
Southampton–Portsmouth
{11..20=3, 21..30=4, 31..40=3, 41..50=3, 51..60=4, 61..70=5}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论