使用 POJO 循环行以创建缺少项目的 JSON
Loop rows using POJO to create JSON with missing items
我从数据库中得到以下行:
ID name address1 address 2
-----------------------------------
123 Edvin Hong Kong Hong Kong
123 Edvin Taipei Taiwan
124 Advin Bangkok Thailand
-----------------------------------
我想要以下 JSON 结果:
"Item":[
{ "name": "Edvin"
"addresses": [
{
"address1": "Hong Kong"
"address2": "Hong Kong"
} ,
{
"address1": "Taipei"
"address2": "Taiwan"
}
]
},
{ "name": "Advin"
"addresses": [
{
"address1": "Bangkok"
"address2": "Thaland"
}
]
}
]
我试过这样做:
List<Item> items= new ArrayList<Item>();
List<String> ids = new ArrayList<String>();
for (Record record: records) { // Loop the rows show above
if(!ids .contains(record.getId())) { //prevent duplicate Item
Item item = new Item();
item.setName(record.getSurname());
Address address = new Address();
address.setAddress1 = record.getAddress1();
address.setAddress2 = record.getAddress2();
item.setAddreses(address);
items.add(item); // add the item into items
}
Ids.add(record.getId());
}
上面的代码我只能获取商品名Edvin的第一个地址,如何获取Edvin的第二个地址?
首先,您创建的对象Item不适合您要获取的JSON。据我所知,对象 Item 有一个 属性 类型的地址,可以跟踪地址 1 和地址 2,但是如果你想要上面的 JSON 结果,你需要一个地址列表作为属性 代替。
然后,如果您想保留 List 项目和 List id 并假设您正在收集所有具有相同名称的项目,代码应该这样更改:
List<Item> items= new ArrayList<Item>();
List<String> ids = new ArrayList<String>();
for (Record record: records) { // Loop the rows show above
if(!ids .contains(record.getId())) { //prevent duplicate Item
Item item = new Item();
item.setName(record.getSurname());
Address address = new Address();
address.setAddress1 = record.getAddress1();
address.setAddress2 = record.getAddress2();
List<Address> addrList = new ArrayList<Address>();
addrList.add(address);
item.setAddreses(addrList);
items.add(item); // add the item into items
} else {
for (Item it: items) {
if(it.getName().equals(record.getSurname()) {
Address addr = new Address();
addr.setAddress1 = record.getAddress1();
addr.setAddress2 = record.getAddress2();
it.getAddresses().add(addr);
}
}
}
Ids.add(record.getId());
}
确实不是最好的解决方案,在这种情况下,我建议继续使用 Map,根据定义避免重复,而不是使用 List 数据结构。
我从数据库中得到以下行:
ID name address1 address 2
-----------------------------------
123 Edvin Hong Kong Hong Kong
123 Edvin Taipei Taiwan
124 Advin Bangkok Thailand
-----------------------------------
我想要以下 JSON 结果:
"Item":[
{ "name": "Edvin"
"addresses": [
{
"address1": "Hong Kong"
"address2": "Hong Kong"
} ,
{
"address1": "Taipei"
"address2": "Taiwan"
}
]
},
{ "name": "Advin"
"addresses": [
{
"address1": "Bangkok"
"address2": "Thaland"
}
]
}
]
我试过这样做:
List<Item> items= new ArrayList<Item>();
List<String> ids = new ArrayList<String>();
for (Record record: records) { // Loop the rows show above
if(!ids .contains(record.getId())) { //prevent duplicate Item
Item item = new Item();
item.setName(record.getSurname());
Address address = new Address();
address.setAddress1 = record.getAddress1();
address.setAddress2 = record.getAddress2();
item.setAddreses(address);
items.add(item); // add the item into items
}
Ids.add(record.getId());
}
上面的代码我只能获取商品名Edvin的第一个地址,如何获取Edvin的第二个地址?
首先,您创建的对象Item不适合您要获取的JSON。据我所知,对象 Item 有一个 属性 类型的地址,可以跟踪地址 1 和地址 2,但是如果你想要上面的 JSON 结果,你需要一个地址列表作为属性 代替。
然后,如果您想保留 List 项目和 List id 并假设您正在收集所有具有相同名称的项目,代码应该这样更改:
List<Item> items= new ArrayList<Item>();
List<String> ids = new ArrayList<String>();
for (Record record: records) { // Loop the rows show above
if(!ids .contains(record.getId())) { //prevent duplicate Item
Item item = new Item();
item.setName(record.getSurname());
Address address = new Address();
address.setAddress1 = record.getAddress1();
address.setAddress2 = record.getAddress2();
List<Address> addrList = new ArrayList<Address>();
addrList.add(address);
item.setAddreses(addrList);
items.add(item); // add the item into items
} else {
for (Item it: items) {
if(it.getName().equals(record.getSurname()) {
Address addr = new Address();
addr.setAddress1 = record.getAddress1();
addr.setAddress2 = record.getAddress2();
it.getAddresses().add(addr);
}
}
}
Ids.add(record.getId());
}
确实不是最好的解决方案,在这种情况下,我建议继续使用 Map,根据定义避免重复,而不是使用 List 数据结构。