I am sending few 'fields' and 'lists' in JSON to Spring MVC Controller as below:
var data = {
'message' : 'Text data',
'**listOfIds**' : '350234983, 378350950',
'synchronizerToken' : formTokenId
};
$.ajax({
url : 'testURL.do',
type : 'post',
data : data,
cache : false,
dataType : 'json',
success : function (jsonResponse) {},
error : function (error) {}
});
In Spring MVC controller the URL handler looks like this:
@RequestMapping(value = "/testURL.do", method = RequestMethod.POST)
public ModelAndView executeTest( ListData listData) {
ModelAndView modelAndView = null;
JsonResponse jsonResponse = null;
modelAndView = executeTransaction(listData);
}
return modelAndView;
}
ListData.java
public class ListData{
private String message;
private List<String> **listOfIds** = new ArrayList<String>();
//getter/setters
The issue is listOfIds is not being returned as list. It is returned as single string '350234983, 378350950'
Can anyone suggest if anything is wrong here or is there any better way to receive list in JSON response?
Thanks, -Fonda
Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection. But it puts JSON representation of your object as String. So you should use either JSONArrays or Java objects. You should not mix them.
Similar to other programming languages, a JSON Array is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma.
To convert a list to json in Python, use the json. dumps() method. The json. dumps() is a built-in function that takes a list as an argument and returns the json value.
Make listOfIds an array of strings instead of a single string.
'listOfIds' : ['350234983', '378350950'],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With