Javascript JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13916673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue with JSON.stringify adding a extra \ and "" to my Json object
提问by Devsined
Hi I am creating using Javascript an array of object with a key and a value using the following code.
嗨,我正在使用 Javascript 创建一个带有键和值的对象数组,使用以下代码。
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
当我尝试对其进行字符串化并在帖子中发送该 JSON 时,我在我不想要的地方使用了格式错误的 JSON 和 \ ,因此当我尝试将该 JSON 作为 JObject 使用 C# 进行代码隐藏时,我遇到了问题. 如何使用 stringify 创建干净的 JSON
var jObject = JSON.stringify(ValuesArray);
My JSON now which is wrong is:
我现在错误的 JSON 是:
{
"JObject": "[{\"key\":\"29\",\"value\":\"Country\"}, {\"key\":\"30\",\"value\":\"4,3,5\"}]"
}
I would like to have a JSON object like this
我想要一个像这样的 JSON 对象
{
"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:
更详细地说明我如何将 JSON 发送到我的 API 这是我将 JSON 发送到我的 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
这就是我在 API 控制器中接收它的方式
...
// POST api/datavalues/5
public string Post(int id, JObject value)
{
var temp = value;
...
采纳答案by NG.
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)
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.
发生的事情是您对您的值数组进行了双重 json 编码 - 请注意,您的“无效”JSON 值实际上是一个 JSON 字符串,而不是您想要的数组。
EDITIt 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/
编辑看起来您将地图的 JSON 字符串粘贴到数组中,然后对其进行字符串化。这是一个 jsfiddle,可以帮助您获得所需的内容 - 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.
注意数据属性的变化。这是一个自动为您 JSONified 的值。
回答by sammy
const config = {a: 1, b: 2}
console.log(JSON.stringify(JSON.stringify(config)))
"{\"a\": 1, \"b\": 2}"
"{\"a\": 1, \"b\": 2}"
回答by Thibaut
May be you have an old prototypelibrary.
As I remove it, bug has disappeared
可能你有一个旧prototype图书馆。当我删除它时,错误消失了

