在进一步探索的基础上进行更新,感谢@pinaka30 的评论-
我想验证我是否将 JSON 数组反序列化为 List,是否会保留序列。
我拿了下面的 JSON
{
"MyJSON": {
"name": "Any Name",
"values": [
"1111",
"2222",
"3333",
"4444",
"5555",
"6666"
]
}
}
我创建的 DTO 如下:
package com.jsonrelated.jsonrelated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"MyJSONMapping"
})
public class MyJSONMapping {
@JsonProperty("MyJSON")
@Valid
@NotNull
private MyJSON myJSON;
@JsonProperty("MyJSON")
public MyJSON getMyJSON() {
return myJSON;
}
@JsonProperty("MyJSON")
public void setMyJSON(MyJSON myJSON) {
this.myJSON = myJSON;
}
}
package com.jsonrelated.jsonrelated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"values"
})
public class MyJSON {
@JsonProperty("name")
String name;
@JsonProperty("values")
List<String> values;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getValues() {
return values;
}
public void setValues(List<String> values) {
this.values = values;
}
}
然后我创建了一个 REST API 并将该 JSON 作为请求正文发送
@RequestMapping(path="generateMyJSONChart",method=RequestMethod.POST,produces="application/json",consumes="application/json")
public String generateMyJSONChart(@Valid @RequestBody MyJSONMapping myJSONMapping, @RequestParam("type") @NotBlank String type) throws IOException {
List comparedProducts = myJSONMapping.getMyJSON().getValues();
Iterator i = comparedProducts.iterator();
while (i.hasNext())
{
String name = (String) i.next();
System.out.println(name);
}
return "nothing";
}
我正在检查“值”中的值序列是否如
"values": [
"1111",
"2222",
"3333",
"4444",
"5555",
"6666"
]
如果我们将它们反序列化为 List ,则会被保留,如下所示
@JsonProperty("values")
List<String> values;
我已经多次运行它,值中有 100 个元素,我看到订单被保留了。
已经添加了一个好的帖子JSON 列表中元素的顺序是否保留?所以我的似乎是重复的。