JSON.stringify 添加额外的 \ 和“"的问题到我的 Json 对象 [英] Issue with JSON.stringify adding a extra \ and "" to my Json object

查看:38
本文介绍了JSON.stringify 添加额外的 \ 和“"的问题到我的 Json 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 Javascript 创建一个对象数组,其中包含一个键和一个值,使用以下代码.

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,因此当我尝试将该 JSON 作为 JObject 使用 C# 进行代码隐藏时,我我遇到了问题.如何使用 stringify 来创建干净的 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


   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)

发生的事情是您对 values 数组进行了双重 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/

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)  { .. }});

注意数据属性的变化.这是一个自动为您 JSON 化的值.

Note the change in the data attribute. That is a value that is automatically JSONified for you.

这篇关于JSON.stringify 添加额外的 \ 和“"的问题到我的 Json 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆