问题与JSON.stringify增加一个额外的\\和""我的JSON对象 [英] Issue with JSON.stringify adding a extra \ and "" to my Json object
问题描述
您好我正在创建使用Javascript对象的数组键和值使用以下code。
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'
当我试图字符串化并发送JSON在后我有一个错误格式化的JSON以\\和我不想这样的地方,当我尝试desirales了JSON作为codebehind一个JObject用C#我有麻烦了。我如何使用字符串化创造一个干净的JSON
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);
现在我的JSON这是错误的是:
My JSON now which is wrong is:
{
"JObject": "[{\"key\":\"29\",\"value\":\"Country\"}, {\"key\":\"30\",\"value\":\"4,3,5\"}]"
}
我想有这样的JSON对象
I would like to have a JSON object like this
{
"JObject": [{"key":"29","value":"Country"},{"key":"30","value":"4,3,5"}]
}
周围没有引号的 []
和字符 \\
什么好办法来解决它。
感谢您
的更多细节该怎么我发送JSON我的API
这就是我如何发送JSON到我的Web API:
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);
}
})
}
和这个我怎么在我的API控制器接收它
and this how I am receiving it in my API controller
...
// POST API / datavalues / 5
... // POST api/datavalues/5
公共字符串邮政(INT ID,JObject值)
{
VAR TEMP =值;
public string Post(int id, JObject value) { var temp = value;
...
推荐答案
它看起来像你将一个字符串作为地图中的价值。你应该这样做:
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)
发生了什么事是你是你的编码值数组双JSON - 注意,你的无效JSON值实际上是一个JSON字符串,而不是你想要的阵列
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字符串到一个数组,字符串化然后那个。这里有一个的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) { .. }});
请注意数据属性的变化。这是自动JSONified为你的值。
Note the change in the data attribute. That is a value that is automatically JSONified for you.
这篇关于问题与JSON.stringify增加一个额外的\\和""我的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!