Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending list in JSON request

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

like image 663
McQueen Avatar asked Feb 26 '13 21:02

McQueen


People also ask

How can we send list of objects in JSON?

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.

Can JSON contain a list?

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.

How do you send a list as JSON response in Python?

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.


1 Answers

Make listOfIds an array of strings instead of a single string.

'listOfIds' : ['350234983', '378350950'],
like image 55
Brad M Avatar answered Oct 20 '22 07:10

Brad M