英文:
How can I retrieve many values of a same field in a GET request
问题
我有以下JSON要检索:
{
"name": "João",
"name": "Maria",
"name": "José"
}
我这样做了:
ResponseEntity<List<Users>> responseEntityUsers = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Users.class);
但我遇到了错误。
我的Users类如下:
public class Users {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
英文:
I have the json to retrieve below:
{
"name": "João",
"name": "Maria",
"name": "José"
}
I made this way:
ResponseEntity<List<Users>> responseEntityUsers = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Users.class);
But I got error.
My Users class is below:
public class Users {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案1
得分: 1
你需要正确设计你的JSON,它不是格式良好的JSON。应该使用一个特定属性的值数组,就像这样:
{
“names”: [“João”, “Maria”, "José"]
}
请注意,我已经建议性地将属性名称更改为“names”。这在设计JSON以传输数据时是一个良好的做法。
这个更改还会影响你的模型类,它不再是一个字符串,而是一个字符串数组:
public class Users {
private String[] names;
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
}
祝你一切顺利,加油!
英文:
You need to design your JSON correctly, it is not well-formed JSON. It should be using an array of values for a specific attribute, like this:
{
“names”: [“João”, “Maria”, "José"]
}
Notice that I have propositionally changed the attribute name to "names". Which is a good practice when designing your JSON to transport your data.
That change also will impact your Model class, that instead of String must have a String array:
public class Users {
private String[] names;
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
}
I wish you the best, cheers!
答案2
得分: 0
[
{
"name": "João"
},
{
"name": "Maria"
},
{
"name": "José"
}
]
英文:
You need to design your JSON for this class:
[
{
"name":"João"
},{
"name":"Maria"
},{
"name":"José"
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论