Java 如何以Json格式显示包含员工和多个地址的地图
我已经创建了两个类Employee和Address,每个雇员都有多个地址。我想以JSON格式显示这些地址。我尝试了下面的一些代码。您能告诉我如何更改代码以以下格式显示地图值吗?同样,我想显示id为2的员工Java 如何以Json格式显示包含员工和多个地址的地图,java,json,Java,Json,我已经创建了两个类Employee和Address,每个雇员都有多个地址。我想以JSON格式显示这些地址。我尝试了下面的一些代码。您能告诉我如何更改代码以以下格式显示地图值吗?同样,我想显示id为2的员工 { id: 1 name: John Addresses: [ { addrLine1:StreetABC Line 1 addrLine2: StreetABC Line 2 city:CityABC zip: ZipABC }, { addr
{
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
}
]
}
这是测试类
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();
}
}
}
Address.class
like
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;
}
}
而不是在员工模型中使用MapAddAddress类
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;
}
}
class员工{
私有int-id;
私有字符串名称;
私人名单地址;
公共雇员(整数id,字符串名称){
this.id=id;
this.name=名称;
}
公共无效集合id(内部id){
这个.id=id;
}
公共int getId(){
返回id;
}
公共void集合名(字符串名){
this.name=name;
}
公共字符串getName(){
返回名称;
}
公共列表getAddresses()
{
返回地址;
}
公共列表集合地址(列表地址)
{
这个。地址=地址;
}
}
您的Address类保持不变
改变你的考试等级
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();
}
}
}
公共类测试{
公共静态void main(字符串参数[]){
ObjectMapper mapper=新的ObjectMapper();
员工e1=新员工(1,“约翰”);
员工e2=新员工(2,“罗伯特”);
地址a1=新地址(“StreetABC 1号线”、“StreetABC 2号线”、“CityABC”、“ZipABC”);
地址a2=新地址(“街道XYZ 1号线”、“街道XYZ 2号线”、“城市XYZ”、“ZipXYZ”);
地址a3=新地址(“街道1号线”、“街道2号线”、“城市”、“ZipLMN”);
地址a4=新地址(“街道JQK 1号线”、“街道JQK 2号线”、“城市JQK”、“ZipJQK”);
列表地址1=新的ArrayList();
列表地址2=新的ArrayList();
List employees=new ArrayList();
增加(e1);
增加(e2);
地址1.添加(a1);
地址1.添加(a2);
地址2.添加(a3);
地址2.添加(a4);
e1.设置地址(地址1);
e2.设置地址(地址2);
试一试{
mapper.writeValueAsString(e1);
mapper.writeValueAsString(e2);
}捕获(例外e){
e、 printStackTrace();
}
}
}
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();
}
}
}