Issue with JSON.stringify adding a extra \ and “” to my Json object

后端 未结 3 1765
清歌不尽
清歌不尽 2020-12-03 02:43

Hi I am creating using Javascript an array of object with a key and a value using the following code.

ValuesArray.push({ key: $(this).attr(\'someattribute\')         


        
3条回答
  •  离开以前
    2020-12-03 03:29

    It looks like you are placing a string as the value in your map. You should do something like:

    var objMap = {"JObject" : ValuesArray}; var json = JSON.stringify(objMap)

    What's happening is you are double json encoding your values array - note that your "invalid" JSON value is actually a JSON string rather than the array that you want.

    EDIT It looks like you are sticking in JSON strings of maps into an Array and then stringifying that. Here's a jsfiddle that should help you get what you are looking for - http://jsfiddle.net/4G5nF/

    In your post request, try this

    var jObject = {"JObject" : ValuesArray};
    $.ajax({   url: address,
               type: 'POST',
               dataType: 'json',
               data: jObject,
               success: function (data)  { .. }});
    

    Note the change in the data attribute. That is a value that is automatically JSONified for you.

提交回复
热议问题