英文:
Return response from API as inner JSON objects
问题
以下是翻译后的内容:
// 使用这段代码获取国家的全名和ISO代码列表:
public Map<String, Object> getCountryNameCodeList() {
String[] countryCodes = Locale.getISOCountries();
Map<String, Object> list = new HashMap<>();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
list.put(obj.getDisplayCountry(), obj.getCountry());
}
return list;
}
// REST API:
@GetMapping("shipping_countries")
public ResponseEntity<Map<String, Object>> getShippingCountries() {
Map<String, Object> originalMap = countriesService.getCountryNameCodeList();
List<Map<String, Object>> modifiedList = new ArrayList<>();
for (Map.Entry<String, Object> entry : originalMap.entrySet()) {
Map<String, Object> countryData = new HashMap<>();
countryData.put("name", entry.getKey());
countryData.put("value", entry.getValue());
modifiedList.add(countryData);
}
return new ResponseEntity<>(modifiedList, HttpStatus.OK);
}
希望上述修改的 Java 代码能够满足您的要求,将响应数据转换为所需的格式。
英文:
I use this code to get a list of countries as full name and ISO code:
public Map<String, Object> getCountryNameCodeList() {
String[] countryCodes = Locale.getISOCountries();
Map<String, Object> list = new HashMap<>();
for (String countryCode : countryCodes) {
Locale obj = new Locale("", countryCode);
list.put(obj.getDisplayCountry().toString(), obj.getCountry());
}
return list;
}
Rest API:
@GetMapping("shipping_countries")
public ResponseEntity<Map<String, Object>> getShippingCountries() {
return new ResponseEntity<Map<String, Object>>(countriesService.getCountryNameCodeList(), HttpStatus.OK);
}
I get the response data in this format:
{
"Papua New Guinea": "PG",
"Cambodia": "KH",
"Kazakhstan": "KZ",
"Paraguay": "PY",
.....
}
I would like to get the data this way:
[
{
name: "Papua New Guinea",
value: "PG"
},
{
name: "Unites States",
value: "US"
},
....
]
How I can modify the Java code to return the data this way?
答案1
得分: 3
尝试这个方法。您需要使用数据传输对象来返回定制的数据。
创建一个DTO类。
public class DTO {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "DTO [key=" + key + ", value=" + value + "]";
}
}
在控制器中创建REST API。示例:
@RestController
public class Sample {
@RequestMapping("shipping_countries")
public ResponseEntity<List<DTO>> getShippingCountries() {
Map<String, String> map = new HashMap<>();
map.put("Papua New Guinea", "PG");
map.put("Cambodia", "KH");
List<DTO> list = getCustomisedData(map);
return ResponseEntity.ok(list);
}
private List<DTO> getCustomisedData(Map<String, String> map) {
List<DTO> dtos = new ArrayList();
for (Entry<String, String> value : map.entrySet()) {
DTO dto = new DTO();
dto.setKey(value.getKey());
dto.setValue(value.getValue());
dtos.add(dto);
}
return dtos;
}
}
输出:
英文:
Try this approach. You need to use data transfer object to return customized data.
Create a class DTO.
public class DTO {
private String key;
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "DTO [key=" + key + ", value=" + value + "]";
}
}
Create Rest API in the controller. Example :
@RestController
public class Sample {
@RequestMapping("shipping_countries")
public ResponseEntity<List<DTO>> getShippingCountries() {
Map<String, String> map = new HashMap<>();
map.put("Papua New Guinea", "PG");
map.put("Cambodia", "KH");
List<DTO> list = getCustomisedData(map);
return ResponseEntity.ok(list);
}
private List<DTO> getCustomisedData(Map<String, String> map) {
List<DTO> dtos = new ArrayList();
for(Entry<String, String> value: map.entrySet()) {
DTO dto = new DTO();
dto.setKey(value.getKey());
dto.setValue(value.getValue());
dtos.add(dto);
}
return dtos;
}
}
Output :
答案2
得分: 2
收到的响应是一个地图(map)的JSON
表示,这也是你返回的内容。
如果你想要的json
是一个对象数组,那么如果你想要返回它,最简单的方法就是以这种方式返回,就是从你的地图返回Map.Entry
的集合。类似这样:
@GetMapping("shipping_countries")
public ResponseEntity<Set<Map.Entry<String, Object>>> getShippingCountries() {
return new ResponseEntity<>(countriesService.getCountryNameCodeList().entrySet(), HttpStatus.OK);
}
另一种方法是为响应创建一个Json
序列化器,但这似乎有点过度设计。
英文:
The response you are getting is the JSON
representation of a map, which is what you return.
The json
you want is an array of objects, so if you want to return that- the easiest way will be to return it like that, is to return the set of Map.Entry
from your map. Something like that:
@GetMapping("shipping_countries")
public ResponseEntity<Set<Map.Entry<String, Object>>> getShippingCountries() {
return new ResponseEntity<>(countriesService.getCountryNameCodeList().entrySet(), HttpStatus.OK);
}
Other way can be to create a Json
serializer
for the response, but it seems like an overkill
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论