A JSON string containing double quotes for the value is sent to the controller as NULL

I am making this AJAX call:

function CreateProjectTree(sc) { debugger; $.ajax({ type: "POST", url: "../api/projects/SearchProjects", data: sc, contentType: "application/json; charset=utf-8", dataType: "json" }).done(function(data) { buildTree(data); }).fail(function(jqXHR, status, error) { console.log("Error: " + error); }); } 

If the sc variable does not have double quotes for any of the values ​​(for example, "Person": "Jack" ), it works fine.

However, if the object contains double quotes as such: "Person": ""Jack"" , it will send the object as NULL to the controller.

I am not sure why this is happening.

Should I do something special in this case?

+4
source share
1 answer

"Person": ""Jack"" does not work, because the second quote on ""Jack"" is the final quote (two quotation marks create the string: ""). JSON is bad because of this and therefore is considered null . You can fix it in one of two ways:

1) Change the use of " to ' in JSON: 'Person': '"Jack"'

2) Use escape to use the quote inside the quote: "Person": "\"Jack\""

+2
source

Source: https://habr.com/ru/post/1500834/


All Articles