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

后端 未结 3 1751
清歌不尽
清歌不尽 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:03

    May be you have an old prototype library. As I remove it, bug has disappeared

    0 讨论(0)
  • 2020-12-03 03:19
    const config = {a: 1, b: 2}
    console.log(JSON.stringify(JSON.stringify(config)))
    

    "{\"a\": 1, \"b\": 2}"

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题