java - 如何使用杰克逊流 api 解析给定的 json?
问题描述
输入 json(为 3 名员工采集的样本,但可以有很多员工)。我使用http://www.jsonschema2pojo.org/来获取 pojo 类。
我能够解析除示例员工的联系人数组之外的所有元素。如何在杰克逊解析器方法中解析具有 2 个节点的联系人数组?
JSON -
[
{
"id": 123,
"name": "Pankaj",
"permanent": true,
"address": {
"street": "Albany Dr",
"city": "San Jose",
"zipcode": 95129
},
"phoneNumbers": [
123456,
987654
],
"role": "Manager",
"cities": [
"Los Angeles",
"New York"
],
"contact" : [
{ "type" : "phone/home", "ref" : "333-333-1234"},
{ "type" : "phone/work", "ref" : "444-444-4444"}
]
},
{
"id": 234,
"name": "Test",
"permanent": true,
"address": {
"street": "BBSR Dr",
"city": "San Jose",
"zipcode": 4556
},
"phoneNumbers": [
545614,
54622
],
"role": "SSE",
"cities": [
"Los Angeles",
"New York"
],
"contact" : [
{ "type" : "phone/home", "ref" : "333-333-1234"},
{ "type" : "phone/work", "ref" : "444-444-4444"}
]
},
{
"id": 1231,
"name": "Test123",
"permanent": true,
"address": {
"street": "BBSR Dr",
"city": "Bhubaneswar",
"zipcode": 4556
},
"phoneNumbers": [
545614,
54622
],
"role": "SSE",
"cities": [
"Los Angeles",
"New York",
"Bhubaneswar"
]
"contact" : [
{ "type" : "phone/home", "ref" : "333-333-1234"},
{ "type" : "phone/work", "ref" : "444-444-4444"}
]
}
]
雇员.java
public class Employee {
private Integer id;
private String name;
private Boolean permanent;
private Address address;
private long[] phoneNumbers;
private String role;
private List<String> cities = null;
private List<Contact> contact = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
.
.
.
联系方式.java
public class Contact {
private String type;
private String ref;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
.
.
.
杰克逊解析器方法()
private static void parseJSON(JsonParser jsonParser, Employee emp,
List<Long> phoneNums) throws JsonParseException, IOException {
//loop through the JsonTokens
while(jsonParser.nextToken() != JsonToken.END_OBJECT){
String name = jsonParser.getCurrentName();
if("id".equals(name)){
jsonParser.nextToken();
emp.setId(jsonParser.getIntValue());
}else if("name".equals(name)){
jsonParser.nextToken();
emp.setName(jsonParser.getText());
}else if("permanent".equals(name)){
jsonParser.nextToken();
emp.setPermanent(jsonParser.getBooleanValue());
}else if("address".equals(name)){
jsonParser.nextToken();
//nested object, recursive call
parseJSON(jsonParser, emp, phoneNums);
}else if("street".equals(name)){
jsonParser.nextToken();
emp.getAddress().setStreet(jsonParser.getText());
}else if("city".equals(name)){
jsonParser.nextToken();
emp.getAddress().setCity(jsonParser.getText());
}else if("zipcode".equals(name)){
jsonParser.nextToken();
emp.getAddress().setZipcode(jsonParser.getIntValue());
}else if("phoneNumbers".equals(name)){
jsonParser.nextToken();
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
phoneNums.add(jsonParser.getLongValue());
}
}else if("role".equals(name)){
jsonParser.nextToken();
emp.setRole(jsonParser.getText());
}else if("cities".equals(name)){
jsonParser.nextToken();
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
emp.getCities().add(jsonParser.getText());
}
}else if ("contact".equals(name)){
jsonParser.nextToken();
while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
emp.get
}
}
}
}
解决方案
首先,您的 json 结构中有一个错误。最后一名员工的价值为
"cities": [
"Los Angeles",
"New York",
"Bhubaneswar"
] // here you missed a comma
"contact" : [
{ "type" : "phone/home", "ref" : "333-333-1234"},
{ "type" : "phone/work", "ref" : "444-444-4444"}
]
正确的应该是这样的:
"cities": [
"Los Angeles",
"New York",
"Bhubaneswar"
],
"contact" : [
{ "type" : "phone/home", "ref" : "333-333-1234"},
{ "type" : "phone/work", "ref" : "444-444-4444"}
]
您可以使用下面的 jackson 2 将 json 反序列化为 java 对象。您不需要手动解析。它解析正确。
public class Main {
public static void main(String[]args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
List<Employee> employees = mapper.readValue(new File("test.json"), List.class);
}
}
class Address {
private String street;
private String city;
private Integer zipcode;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getZipcode() {
return zipcode;
}
public void setZipcode(Integer zipcode) {
this.zipcode = zipcode;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
class Contact {
private String type;
private String ref;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
class Employee {
private Integer id;
private String name;
private Boolean permanent;
private Address address;
private List<Integer> phoneNumbers = null;
private String role;
private List<String> cities = null;
private List<Contact> contact = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getPermanent() {
return permanent;
}
public void setPermanent(Boolean permanent) {
this.permanent = permanent;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<Integer> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<Integer> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public List<String> getCities() {
return cities;
}
public void setCities(List<String> cities) {
this.cities = cities;
}
public List<Contact> getContact() {
return contact;
}
public void setContact(List<Contact> contact) {
this.contact = contact;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
推荐阅读
- python - 在 ManytoManyField(Django Admin) models.py 中实现搜索字段
- python - SUMO Traci:遇到障碍物时如何模拟变道
- python - KeyError:“日期”-熊猫
- tomcat - 使用带有 gradle 的嵌入式 tomcat 9 创建可执行的战争
- angular - 通过模板中的 [attr.data-show] 值添加条件
- apache-spark - HDFS中的Pyspark重命名文件
- bash - 如何在 bash 中为邮件分发者准备可变内容?(苹果系统)
- python - 编写一个 void(非结果性)函数来绘制一个正方形。在程序中使用它来绘制如下所示的图像。假设每边是 20 个单位
- python - 如何在熊猫数据框中执行逐行 if 和数学运算
- javascript - “fn 不是函数” discord.js