Server layer will return a list of String value, like
{"Bob", "Charlotte", "Johnson", "David"...}
Now we need the List String to be a Json object to push to front end, like
[{id: "Bob"}, {id: "Charlotte"}, {id: "Johnson"}, {id: "David"...}]
or
[{name: "Bob"}, {name: "Charlotte"}, {name: "Johnson"}, {name: "David"...}]
Any label is fine, we just need a label to make it as JSON object. Does Jackson has something to convert List of String by default ie {string: "Bob"}
? That will be really sweet......
最简单的方法是让你的控制器方法返回一个完全映射到你想要的JSON的结构 - 例如List<SomeObject>
,其中SomeObject
是一个带有String id
字段的类。
Th way of doing this is converting you List of String into a New Map.
Code :
@ResponseBody
@RequestMapping("/mapping")
function mySpring()
{
List<String> myStringList;
Map<String,String> jsonMap= new LinkedHashMap<String,String>();
// Use LinkedHashMap if you want to maintain order
String key="id";
// Id or Name
for (String myString: myStringList)
{
jsonMap.put(key,myString);
}
return jsonMap;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.