使用Java进行Json数据的解析(对象数组的相互嵌套)有多种方式,其中一种较为常用的方式是通过Jackson库进行解析。以下是使用Jackson库进行Json数据解析的完整攻略:
步骤一:引入Jackson库
在pom.xml中引入Jackson库的dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
步骤二:解析Json数据
假设有如下Json数据:
{
"id": 1,
"name": "John",
"age": 30,
"address": {
"city": "New York",
"zip": "10001"
},
"phones": [
{
"type": "home",
"number": "212-555-1234"
},
{
"type": "fax",
"number": "646-555-4567"
}
]
}
我们可以先定义一个Java类来映射Json数据:
public class Person {
private int id;
private String name;
private int age;
private Address address;
private List<Phone> phones;
// 省略getter和setter方法
}
public class Address {
private String city;
private String zip;
// 省略getter和setter方法
}
public class Phone {
private String type;
private String number;
// 省略getter和setter方法
}
然后使用ObjectMapper类将Json数据转换为Java对象:
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"id\":1,\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"New York\",\"zip\":\"10001\"},\"phones\":[{\"type\":\"home\",\"number\":\"212-555-1234\"},{\"type\":\"fax\",\"number\":\"646-555-4567\"}]}";
Person person = objectMapper.readValue(json, Person.class);
现在person对象里就存储了解析后的Json数据。我们可以通过调用getter方法来访问里面的元素,例如:
System.out.println(person.getName()); // 输出 "John"
System.out.println(person.getAddress().getZip()); // 输出 "10001"
System.out.println(person.getPhones().get(1).getNumber()); // 输出 "646-555-4567"
示例1:解析Json数组
如果Json数据中有一个数组,我们可以将它定义为Java List类型,例如:
{
"students": [
{
"name": "John",
"age": 20
},
{
"name": "Mary",
"age": 18
}
]
}
public class School {
private List<Student> students;
// 省略getter和setter方法
}
public class Student {
private String name;
private int age;
// 省略getter和setter方法
}
String json = "{\"students\":[{\"name\":\"John\",\"age\":20},{\"name\":\"Mary\",\"age\":18}]}";
School school = objectMapper.readValue(json, School.class);
现在我们可以通过调用school对象的getter方法来访问里面的元素,例如:
System.out.println(school.getStudents().get(0).getName()); // 输出 "John"
System.out.println(school.getStudents().get(1).getAge()); // 输出 18
示例2:生成Json数据
Jackson库也可以用来生成Json数据。使用ObjectMapper类的writeValueAsString方法可以将Java对象转换为Json字符串:
Person person = new Person();
person.setId(1);
person.setName("John");
person.setAge(30);
Address address = new Address();
address.setCity("New York");
address.setZip("10001");
person.setAddress(address);
List<Phone> phones = new ArrayList<>();
Phone phone1 = new Phone();
phone1.setType("home");
phone1.setNumber("212-555-1234");
phones.add(phone1);
Phone phone2 = new Phone();
phone2.setType("fax");
phone2.setNumber("646-555-4567");
phones.add(phone2);
person.setPhones(phones);
String json = objectMapper.writeValueAsString(person);
此时json字符串的值为:
{
"id": 1,
"name": "John",
"age": 30,
"address": {
"city": "New York",
"zip": "10001"
},
"phones": [
{
"type": "home",
"number": "212-555-1234"
},
{
"type": "fax",
"number": "646-555-4567"
}
]
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用Java进行Json数据的解析(对象数组的相互嵌套) - Python技术站