41

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'), value: $(this).val() });

As a result I have am array of object like this:

key:29; value: 'Country'
Key:12; value: '4,3,5'

when I am trying to stringify it and send that JSON in a post I am having a wrong formatted JSON with \ and " in places that I dont want so when I try to desirales that JSON as a JObject on codebehind with C# I am having trouble. How can I create a clean JSON using the stringify

var jObject = JSON.stringify(ValuesArray);

My JSON now which is wrong is:

{
  "JObject": "[{\"key\":\"29\",\"value\":\"Country\"},  {\"key\":\"30\",\"value\":\"4,3,5\"}]"
}

I would like to have a JSON object like this

{
  "JObject": [{"key":"29","value":"Country"},{"key":"30","value":"4,3,5"}]
}

without the quotes around the [] and the character \

Any good idea to solve it.

Thank you

More detail this how I am sending the JSON to my API this is how I am sending the JSON to my Web API:

function PostAPIRequest(address) {

           var jObject = JSON.stringify(ValuesArray);

           var responseJson = null;
           $.ajax({
               url: address,
               type: 'POST',
               dataType: 'json',
               data: { JObject: jObject },
               success: function (data) {
                   responseJson = data
                   ProcessDataResponse(responseJson);
                   //TODO: REFRESH THE DATA GRID
               },
               error: function (xhr, ajaxOptions, thrownError) {
                   //TODO redirect to the error page and send error email there.
                   alert(xhr.status);
                   alert(thrownError);
               }
           })
       }

and this how I am receiving it in my API controller

...
  // POST api/datavalues/5


   public string Post(int id, JObject  value)
    {
        var temp = value;

...
RBT
  • 24,161
  • 21
  • 159
  • 240
Devsined
  • 3,363
  • 6
  • 30
  • 48

3 Answers3

25

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.

NG.
  • 22,560
  • 5
  • 55
  • 61
  • Thanks SB but it didnt work it create this JSON { "{\"JObject\":": { "{\"key\":\"20\",\"value\":\"ddd\"},{\"key\":\"21\",\"value\":\"4,5,13,14,15,\"}]}": "" } } I am still having the characters \ and no a valid JSON formatted to deserialize in my Web API – Devsined Dec 17 '12 at 15:25
  • It is interesting that now I get a clean JSON but with a lot of spaces and line return – Devsined Dec 17 '12 at 19:40
  • This is wrong, this is not the reason it happens. Its because the JSON string created is: [{"key":"29","value":"Country"},{"key":"30","value":"4,3,5"}]. But when you try to post it to the php, the string you want to send gets placed inside another string, with seporator ". Which means your string cant contain ", and therefore jquery turns them in to \" – Elias Fyksen Jul 27 '17 at 10:18
22
const config = {a: 1, b: 2}
console.log(JSON.stringify(JSON.stringify(config)))

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

sammy
  • 437
  • 4
  • 11
12

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

Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
Thibaut
  • 2,596
  • 2
  • 24
  • 24