Spring json映射具有不同名称的元素以列出

我使用spring boot 2.1.4,我的问题是json映射到对象。是否可以像这样映射json


{

   "name":"test",

   "list1":1,

   "list2":2,

   "list3":3,

   "list4":4,

   "list5":5,

   "list6":6,

   "list7":7

}

像这样反对:


@Data

@NoArgsConstructor

@AllArgsConstructor

public class Test {


    private String name;

    private List<Integer> list;


}

在我的 Spring Boot 控制器中,它看起来像这样:


@GetMapping("/criteria")

public String registration(@RequestBody Test test) {

    return "";

}

有没有办法将此json映射到我的自定义对象?


慕哥6287543
浏览 165回答 2
2回答

慕仙森

您可以为对象制作自定义反序列化器,如下所示。@JsonDeserialize(using = CustomDeserializer::class)data class MyCustomObject(&nbsp; &nbsp; var name: String,&nbsp; &nbsp; var list: List<Int>)class CustomDeserializer: JsonDeserializer<MyCustomObject>(){&nbsp; &nbsp; override fun deserialize(p: JsonParser, ctxt: DeserializationContext): MyCustomObject {&nbsp; &nbsp; &nbsp; &nbsp; var myList = ArrayList<Int>()&nbsp; &nbsp; &nbsp; &nbsp; var myName = ""&nbsp; &nbsp; &nbsp; &nbsp; while(p.nextToken() != JsonToken.END_OBJECT){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(p.currentName() == "name"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myName = p.nextTextValue()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(p.currentName().contains("list")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.add(p.nextIntValue(0))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return MyCustomObject(myName, myList)&nbsp; &nbsp; }}这是用 Kotlin 编写的,如果您需要我的帮助将其转换为 java,请告诉我。编辑继续为您将其转换为 Java。请原谅任何错别字:) 如果您有任何问题或问题,请告诉我@JsonDeserialize(using = CustomDeserializer.class)public class MyCustomObjectAgain {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private List<Integer> myList;&nbsp; &nbsp; public MyCustomObjectAgain(String name, List<Integer> myList) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.myList = myList;&nbsp; &nbsp; }&nbsp; &nbsp; private class CustomDeserializer extends JsonDeserializer<MyCustomObject>{&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public MyCustomObject deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Integer> myList = new ArrayList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String myName = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(p.nextToken() != JsonToken.END_OBJECT){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(p.currentName() == "name"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myName = p.nextTextValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(p.currentName().contains("list")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myList.add(p.nextIntValue(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new MyCustomObject(myName, myList);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

开心每一天1111

如果List你可以考虑@JsonAnySetter并且在你需要的程序中做properties.values()允许我们灵活地使用 Map 作为标准属性。在反序列化时,来自 JSON 的属性将简单地添加到地图中。@Data@NoArgsConstructor@AllArgsConstructorpublic class Test {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private Map<String, String> properties;&nbsp; &nbsp; @JsonAnySetter&nbsp; &nbsp; public void add(String key, String value) {&nbsp; &nbsp; &nbsp; &nbsp;properties.put(key, value);&nbsp; &nbsp; &nbsp;}}您也可以尝试List使用@JsonAnySetter@Data@NoArgsConstructor@AllArgsConstructorpublic class Test {&nbsp; &nbsp; private String name;&nbsp; &nbsp; private List<Integer> properties;&nbsp; &nbsp; @JsonAnySetter&nbsp; &nbsp; public void add(String key, Integer value) {&nbsp; &nbsp; &nbsp; &nbsp;properties.add(value);&nbsp; &nbsp; &nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java