简体   繁体   English

Json.Net解析日期时间值错误

[英]Json.Net parsing datetime value error

I am trying to convert a json string to a JObject using JObject.Parse, but running into the error "Error parsing positive infinity value. Path 'Modified.Date', line 1, position 52." 我试图使用JObject.Parse将json字符串转换为JObject,但是遇到错误“解析正无穷大值时出错。路径'Modified.Date',第1行,位置52。”

Here is the part of json that is throwing the error on - 这是JSON引发错误的部分-

{ ..., "Modified" : { "Date" : ISODate("2013-02-21T22:23:57.118Z"), "User" : "Admin" }, ...} {...,“ Modified”:{“ Date”:ISODate(“ 2013-02-21T22:23:57.118Z”),“ User”:“ Admin”},...}

Here is the code I am using to do the parse - 这是我用来解析的代码-

var jobj = JObject.Parse(formJson)

Update: The json was generated by using mongodb's .ToJson() extension method, by sending in the following jsonwritersettings it generated json that was parsable by json.net - new JsonWriterSettings { OutputMode = JsonOutputMode.JavaScript }; 更新: json是使用mongodb的.ToJson()扩展方法生成的,通过发送以下jsonwritersettings生成了可由json.net解析的json-new JsonWriterSettings {OutputMode = JsonOutputMode.JavaScript};

I think you need to lose the ISODate. 我认为您需要丢失ISODate。

This works: 这有效:

String MyJson = "{MyDate   : \"2013-02-21T22:23:57.118Z\" }";
var x = Newtonsoft.Json.Linq.JObject.Parse(MyJson);

I tried using Regex and convert in C#: 我尝试使用Regex并在C#中进行转换:

Regex _regex = new Regex(@"\d\d\d\d-\d\d-\d\d");
                    Match _date = _regex.Match(<Your_Date_String>);
                    if (_date.Success)
                    {
                        var datetime = Convert.ToDateTime(_date.Value);
                    }

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

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