标签: stringify

在反序列化期间将JSON日期转换为.NET DateTime的正确方法

我有一个javascript函数,用JSON数据调用MVC控制器:

var specsAsJson = JSON.stringify(specs);
$.post('/Home/Save', { jsonData: specsAsJson });
Run Code Online (Sandbox Code Playgroud)

在服务器端,在控制器内,我似乎无法通过此错误:

/ Date(1347992529530)/不是DateTime的有效值.

当我调用Deserialize()(下面的方法中的第三行)时会发生异常:

    public ActionResult Save(string jsonData)
    {
        var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new[] { new TimeSpanJsonConverter() });
        var specs = serializer.Deserialize<List<EquipmentSpecWithParameterlessConstructor>>(jsonData);

        return View("Index", _allTrackerJobs);
    }
Run Code Online (Sandbox Code Playgroud)

我一直在做一些谷歌搜索,上面的代码是我最近尝试使这项工作(从这里使用TimeSpanJsonConverter ).其他方法显示只向服务器发送日期,但我有一个对象列表,其中日期作为一些属性.

是否有一种优雅的,普遍接受的方法来解决这个问题,还是我们还需要某种丑陋的解决方法?解决这个问题的正确方法是什么?

===================原始问题结束===================


编辑 - 通过使用JsonConvert序列化解决

请参阅下面的答案(不是这个问题中糟糕的解决方法).


编辑 - 糟糕的解决方法

我创建了一个DTO,其字段与域对象完全相同,只是我将日期字段设置为字符串,以便反序列化.现在我可以反序列化它,我将努力将日期转换为有效的格式,以便我可以从我的DTO创建域对象.

public class EquipmentSpecDto
{
    public string StartTime { get; set; }
    public string EndTime { get; set; }
    // more properties here
}
Run Code Online (Sandbox Code Playgroud)

我只是使用DTO进行反序列化:

var specs = …
Run Code Online (Sandbox Code Playgroud)

javascript c# json stringify

15
推荐指数
2
解决办法
5万
查看次数

JSON.stringify将Infinity转换为null

我有JavaScript对象说:

var a = {b: Infinity, c: 10};
Run Code Online (Sandbox Code Playgroud)

当我做

var b = JSON.stringify(a);
Run Code Online (Sandbox Code Playgroud)

它返回以下内容

b ="{"b":null,"c":10}";

JSON.stringify如何将对象转换为字符串?

我尝试过MDN解决方案.

function censor(key, value) {
  if (value == Infinity) {
    return "Infinity";
  }
  return value;
}
var b = JSON.stringify(a, censor);
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,我必须返回字符串"Infinity" Infinity.如果我返回Infinity,它会再次将Infinity转换为null.

我该如何解决这个问题.

javascript json infinity stringify

15
推荐指数
3
解决办法
6465
查看次数

JSON.stringify忽略对象属性

请参阅jsfiddle示例http://jsfiddle.net/frigon/H6ssq/

由于某种原因,JSON.stringify忽略了一些字段.有没有办法强制JSON.stringify解析它们?

正如jsfiddle所示......这段代码......

<script src="http://cdn.kendostatic.com/2012.2.710/js/kendo.all.min.js"></script>
    <script>
    var model = kendo.data.Model.define({id: "ID", fields: {"Name":{type: "string"}}});
    var obj = new model();
    obj.set("Name","Johhny Foosball");
    document.write("<br />obj.dirty property exists: ");
    document.write(obj.dirty);
    document.write("<br/>obj.uid property exists: ");
    document.write(obj.uid);
    document.write("<br/>But they dont show in JSON.stringify():<br/>");    
    document.write(JSON.stringify(obj));
</script>
Run Code Online (Sandbox Code Playgroud)

将输出:

obj.dirty属性存在:true

obj.uid属性存在:b4af4dfc-9d94-4a2d-b286-d6f4cbc991d8

但它们不会在JSON.stringify()中显示:

{"ID":"","名称":"Johhny Foosball"}

javascript json stringify kendo-ui

14
推荐指数
1
解决办法
9215
查看次数

Json - stringify以便数组在一行上

是否可以将JSON对象字符串化为这样,数组在一行中 - 而不是缩进

{
    "Repeat": {
        "Name": [["Top_level","All"],[[1,1]]],
        "Link": [["Top_level"],[[1,1]]]
    },
    "Delete": ["Confirm","Cancel"],
    "Move": ["Up","Down"],
    "Number": ["Ascending","Descending"]
}
Run Code Online (Sandbox Code Playgroud)

json stringify

13
推荐指数
6
解决办法
8970
查看次数

有没有办法删除C宏中的引号?

假设我想取消应该转换"text"为的宏参数的字符串text.

#define UN_STRINGIFY(x) /* some macro magic here */
Run Code Online (Sandbox Code Playgroud)

现在调用此宏""将从其参数中删除

UN_STRINGIFY("text") // results in ----> text
Run Code Online (Sandbox Code Playgroud)

这与宏字符串化相反:

#define STRINGIFY(x) #x
Run Code Online (Sandbox Code Playgroud)

这是可能的,还是我在玩宏观邪恶?

c c++ macros stringify

13
推荐指数
1
解决办法
8237
查看次数

为什么JSON.stringify对于似乎具有属性的对象返回空对象符号"{}"?

以下示例显示JSON.stringify()返回"{}"SpeechSynthesisVoice对象的字符串:

var voiceObject = window.speechSynthesis.getVoices()[0];
JSON.stringify(voiceObject); //returns "{}"?
Run Code Online (Sandbox Code Playgroud)

完整示例:JSFiddle

它为什么会返回,"{}"而不是像"{voiceURI: "Google Deutsch", name: "Google Deutsch", lang: "de-DE", localService: false, default: false}"什么?

请注意,上面的示例不适用于chrome或iOS; 它针对的是Mozilla Firefox.

javascript json text-to-speech stringify

12
推荐指数
2
解决办法
7470
查看次数

JSON.stringify忽略一些对象成员

这是一个简单的例子.

function Person() {
  this.name = "Ted";
  this.age = 5;
}

persons[0] = new Person();
persons[1] = new Person();
JSON.stringify(persons);
Run Code Online (Sandbox Code Playgroud)

如果我有一个Person对象数组,我想将它们字符串化.如何只返回名称变量的JSON.

原因是,我有大型对象的递归引用导致问题.我想从stringify过程中删除递归变量和其他变量.

谢谢你的帮助!

javascript jquery json stringify

11
推荐指数
3
解决办法
1万
查看次数

NodeJS JSON.stringify()瓶颈

我的服务返回非常大的JSON对象的响应 - 大约60MB.经过一些分析后,我发现它几乎一直在进行JSON.stringify()调用,用于转换为字符串并将其作为响应发送.我尝试过stringify的自定义实现,它们甚至更慢.

这对我的服务来说是一个瓶颈.我希望能够每秒处理尽可能多的请求 - 目前1个请求需要700毫秒.

我的问题是:
1)我可以优化响应部分的发送吗?有没有比对字符串化和发送响应更有效的方法?

2)使用异步模块并JSON.stringify()在单独的线程中执行会改善整体请求数/秒(假设超过90%的时间花在该呼叫上)?

javascript json node.js stringify

11
推荐指数
2
解决办法
1万
查看次数

JSON中的JSON字符串

我想在JSON请求中创建一个JSON字符串.这是我的代码,

小提琴

JS

var x = {
    a: 1,
    b: 'a sample text',
};

var request = {
    t: JSON.stringify(x),
    c: 2,
    r: 'some text'
};

console.log(request);
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我如何逃避双引号?

安慰

Object {
  t: "{"a":1,"b":"a sample text"}", //This creates a problem, double quotes inside double quotes.
  c: 2, 
  r: "some text"
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

javascript json escaping stringify

11
推荐指数
2
解决办法
3万
查看次数

Aurelia aurelia-fetch-client和JSON POST

我有以下代码,其工作正常:

import {inject} from 'aurelia-framework';
import {HttpClient, json} from 'aurelia-fetch-client';


@inject(HttpClient)
export class Items {
  heading = 'Items';
  apiKey = "";

  constructor(http) {
    http.configure(config => {
      config
        .useStandardConfiguration()
        .withBaseUrl('https://testme.com/api/')
          .withDefaults({
            headers: {
              'content-type': 'application/json',
              'Accept': 'application/json',
              'X-Requested-With': 'Fetch'
            }
          })
    });

    this.http = http;
  }

attach() {

let auth = {
  Username:"admin",
  Password:"1234"
};

return this.http.fetch('auth', {
      method: 'post',
      body: JSON.stringify(auth),
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }
    })
    .then(response => response.json())
    .then(response => {
      this.apiKey = response.APIKey;
      console.log(response);
}); …
Run Code Online (Sandbox Code Playgroud)

json stringify aurelia

10
推荐指数
1
解决办法
8176
查看次数

标签 统计

stringify ×10

json ×9

javascript ×7

aurelia ×1

c ×1

c# ×1

c++ ×1

escaping ×1

infinity ×1

jquery ×1

kendo-ui ×1

macros ×1

node.js ×1

text-to-speech ×1