我正在尝试在JSON请求中使用JSON。
例如:
{
"name":"newdeeeepaajlf",
"category":"fsafaa",
"jsonData":{
"a":"value"
}
}
现在,当我尝试将其放入我的DTO中时,
private JSONObject jsonData;
它被转换为空白JSON
{}
我陷入了困境。
我们可以用地图来转换数据
public class TestModel {
private String name;
private String category;
private Map<String, Object> jsonObj;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Map<String, Object> getJsonObj() {
return jsonObj;
}
public void setJsonObj(Map<String, Object> jsonObj) {
this.jsonObj = jsonObj;
}
}
并使用像下面这样的控制器上的类
@PostMapping("/test")
public boolean test(@RequestBody TestModel model) {
System.out.println(model.getCategory());
System.out.println(model.getName());
JSONObject jsonObj = new JSONObject(model.getJsonObj());
System.out.println(jsonObj);
return true;
}
要求
{
"category":"json",
"name":"name",
"jsonObj": {
"a": "value"
}
}
它将打印
json
name
{a=value}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句