简体   繁体   English

使用JSON.stringify向我的Json对象添加一个额外的\和“”

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

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与\\和“在我不想要的地方,所以当我尝试在C#的代码隐藏中使用JSON作为JObject时,我遇到了麻烦如何使用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;

...

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. 发生了什么事情你是对你的值数组进行双json编码 - 注意你的“无效”JSON值实际上是一个JSON字符串而不是你想要的数组。

EDIT It looks like you are sticking in JSON strings of maps into an Array and then stringifying that. 编辑看起来你正在将JSON字符串的地图粘贴到一个数组中然后对其进行字符串化。 Here's a jsfiddle that should help you get what you are looking for - http://jsfiddle.net/4G5nF/ 这里有一个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的值。

May be you have an old prototype library. 可能你有一个旧的prototype库。 As I remove it, bug has disappeared 当我删除它时,bug已经消失了

const config = {a: 1, b: 2}
console.log(JSON.stringify(JSON.stringify(config)))

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM