我创建了两个类Employee和Address,每个雇员将有多个地址。我想以JSON格式显示这些地址。我在下面尝试了一些代码。您能否让我知道如何更改代码以显示以下格式的地图值?同样,我想显示ID为2的员工。
{
id: 1
name: John
Addresses: [
{
addrLine1:StreetABC Line 1
addrLine2: StreetABC Line 2
city:CityABC
zip: ZipABC
},
{
addrLine1:StreetXYZ Line 1
addrLine2: StreetXYZ Line 2
city:CityXYZ
zip: ZipXYZ
}
]
}
这里是Test.class
;
import java.util.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
public class Test {
public static void main(String args[]) {
ObjectMapper mapper = new ObjectMapper();
Employee e1= new Employee(1,"John");
Employee e2= new Employee(2,"Robert");
Address a1 = new Address ("StreetABC Line 1","StreetABC Line 2", "CityABC","ZipABC");
Address a2 = new Address ("StreetXYZ Line 1","StreetXYZ Line 2", "CityXYZ","ZipXYZ");
Address a3 = new Address ("StreetLMN Line 1","StreetLMN Line 2", "CityLMN","ZipLMN");
Address a4 = new Address ("StreetJQK Line 1","StreetJQK Line 2", "CityJQK","ZipJQK");
List<Address> address1=new ArrayList<Address>();
List<Address> address2=new ArrayList<Address>();
List<Employee> employees = new ArrayList<Employee>();
employees.add(e1);
employees.add(e2);
address1.add(a1);
address1.add(a2);
address2.add(a3);
address2.add(a4);
Map<Employee, List<Address>> employeeAddressmap=new HashMap<Employee, List<Address>>();
employeeAddressmap.put(e1,address1);
employeeAddressmap.put(e2,address2);
try {
mapper.writeValueAsString(employeeAddressmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Employee.class
喜欢;
class Employee{
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
Address.class
喜欢;
class Address{
String addrLine1;
String addrLine2;
String city;
String zipcode;
public Address(String addrLine1, String addrLine2, String city, String zipcode) {
this.addrLine1 = addrLine1;
this.addrLine2 = addrLine2;
this.city = city;
this.zipcode = zipcode;
}
public String getAddrLine1() {
return addrLine1;
}
public void setAddrLine1(String addrLine1) {
this.addrLine1 = addrLine1;
}
public String getAddrLine2() {
return addrLine2;
}
public void setAddrLine2(String addrLine2) {
this.addrLine2 = addrLine2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
}
答案 0 :(得分:0)
在您的Employee模型中,而不是使用Map添加地址类。
class Employee{
private int id;
private String name;
private List<Address> addresses;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public List<Address> getAddresses()
{
return addresses;
}
public List<Address> setAddresses(List<Address> addresses)
{
this.addresses=addresses;
}
}
您的地址类别保持不变
更改测试课程
public class Test {
public static void main(String args[]) {
ObjectMapper mapper = new ObjectMapper();
Employee e1= new Employee(1,"John");
Employee e2= new Employee(2,"Robert");
Address a1 = new Address ("StreetABC Line 1","StreetABC Line 2", "CityABC","ZipABC");
Address a2 = new Address ("StreetXYZ Line 1","StreetXYZ Line 2", "CityXYZ","ZipXYZ");
Address a3 = new Address ("StreetLMN Line 1","StreetLMN Line 2", "CityLMN","ZipLMN");
Address a4 = new Address ("StreetJQK Line 1","StreetJQK Line 2", "CityJQK","ZipJQK");
List<Address> address1=new ArrayList<Address>();
List<Address> address2=new ArrayList<Address>();
List<Employee> employees = new ArrayList<Employee>();
employees.add(e1);
employees.add(e2);
address1.add(a1);
address1.add(a2);
address2.add(a3);
address2.add(a4);
e1.setAddresses(address1);
e2.setAddresses(address2);
try {
mapper.writeValueAsString(e1);
mapper.writeValueAsString(e2);
} catch (Exception e) {
e.printStackTrace();
}
}
}