如何在GET请求中检索同一字段的多个值

huangapple go评论149阅读模式
英文:

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:

{
  &quot;name&quot;: &quot;Jo&#227;o&quot;,
  &quot;name&quot;: &quot;Maria&quot;,
  &quot;name&quot;: &quot;Jos&#233;&quot;
}

I made this way:

  ResponseEntity&lt;List&lt;Users&gt;&gt; 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&#227;o”, “Maria”, &quot;Jos&#233;&quot;]
}

请注意,我已经建议性地将属性名称更改为“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&#227;o”, “Maria”, &quot;Jos&#233;&quot;]
}

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:

[
    {
    &quot;name&quot;:&quot;Jo&#227;o&quot;
    },{
    &quot;name&quot;:&quot;Maria&quot;
    },{
    &quot;name&quot;:&quot;Jos&#233;&quot;
    }
]

huangapple
  • 本文由 发表于 2020年7月22日 12:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63026675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定