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'

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);

My JSON now which is wrong is:

{
  "JObject": "[{\"key\":\"29\",\"value\":\"Country\"},  {\"key\":\"30\",\"value\":\"4,3,5\"}]"
}

我希望有一个这样的JSON对象

{
  "JObject": [{"key":"29","value":"Country"},{"key":"30","value":"4,3,5"}]
}

without the quotes around the [] and the character \

有什么好主意可以解决吗.

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:

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控制器中接收它的方式

...
  // POST api/datavalues/5


   public string Post(int id, JObject  value)
    {
        var temp = value;

...

推荐答案

看起来您正在 map 中放置一个字符串作为值.你应该这样做:

var objMap = {"JObject" : ValuesArray}; var json = JSON.stringify(objMap)

现在的情况是,您正在对值数组进行双重json编码——请注意,您的"无效"json值实际上是一个json字符串,而不是您想要的array.

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/

在你的发帖请求中,试试这个

var jObject = {"JObject" : ValuesArray};
$.ajax({   url: address,
           type: 'POST',
           dataType: 'json',
           data: jObject,
           success: function (data)  { .. }});

请注意数据属性中的更改.这是一个自动为您指定的值.

Json相关问答推荐

如何编写MongoDB查询以返回数组数组

在PowerShell中,如何获取数据对所在的JSON对象的名称

如何让JSON子查询在没有行的情况下返回空数组而不是NULL

如何在JSONata对象中迭代并向数组添加新字段?

规范化JSON数据

如何避免解析 ISuperObject 类型字段中的 json 对象

如何在 terraform 输出中打印一组用户信息

在Flutter 中将 map 的 Json 转换为 list

传统编程语言等价于动态 SQL

JSON.NET 中特定对象的自定义转换

如何使用 CORS 实现 JavaScript Google Places API 请求

如何从 mysql 数据库构建 JSON 数组

Spring MVC 4:application/json内容类型设置不正确

了解 JSON Schema 草稿版本 4 中的additionalProperties关键字

Rails 中奇怪的 JSON Javascript 问题

有必要清理 JSON 吗?

gson:将 null 视为空字符串

如何让 javascript 从 .json 文件中读取?

Django:TypeError:[] 不是 JSON 可序列化的为什么?

Javascript:如何判断 AJAX 响应是否为 JSON