JToken: Get raw/original JSON value(JToken:获取原始/原始 JSON 值)
问题描述
有没有办法从 JToken 中获取原始/原始 JSON 值?
Is there a way to get the raw/original JSON value from a JToken?
问题:
var data = JObject.Parse(@"{
    ""SimpleDate"":""2012-05-18T00:00:00Z"",
    ""PatternDate"":""2012-11-07T00:00:00Z""
}");
var value = data["SimpleDate"].Value<string>();
value 现在是 05/18/2012 00:00:00 但我需要原始字符串 2012-05-18T00:00:00Z代码>.
The value is now 05/18/2012 00:00:00 but I need the original string 2012-05-18T00:00:00Z. 
有没有办法获得这个原始值?此外,我无法更改 JObject 的创建方式(例如更改设置),因为它作为参数传递给我的类...
Is there a way to get this original value? Also, I cannot change the way how the JObject is created (e.g. change settings), because it is passed as parameter into my class...
(参考:原NJsonSchema问题)
推荐答案
无法获取原始字符串,日期字符串被识别并转换为 DateTime 结构体/www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonReader.htm" rel="nofollow noreferrer">JsonReader 本身.如果你这样做,你会看到这个:
You cannot get the original string, date strings are recognized and converted to DateTime structs inside the JsonReader itself.  You can see this if you do:
Console.WriteLine(((JValue)data["SimpleDate"]).Value.GetType()); // Prints System.DateTime
但是,您可以通过执行以下操作以 ISO 8601 格式提取日期:
You can, however, extract the dates in ISO 8601 format by doing:
var value = JsonConvert.SerializeObject(data["SimpleDate"]);
// value is "2012-05-18T00:00:00Z"
这将始终以适合 JSON 的字符串格式输出 JValue.由于您的原始日期采用这种格式,这可能会满足您的需求.
This will always output a JValue in a JSON-appropriate string format.  Since your original dates are in this format, this may meet your needs.
(老实说,我很惊讶 JValue.ToString() 以非 ISO 格式输出日期,因为 JObject.ToString() 以 ISO 格式输出包含的日期.)
(Honestly, I'm surprised JValue.ToString() outputs dates in non-ISO format, given that JObject.ToString() does output contained dates in ISO format.)
如果您能够在阅读 JObject 时更改设置,则可以使用 JsonSerializerSettings.DateParseHandling = DateParseHandling.None 禁用 DateTime 识别:
If you were able to change you settings while reading your JObject, you could use JsonSerializerSettings.DateParseHandling = DateParseHandling.None to disable DateTime recognition:
var settings = new JsonSerializerSettings { DateParseHandling = DateParseHandling.None };
var data = JsonConvert.DeserializeObject<JObject>(@"{
    ""SimpleDate"":""2012-05-18T00:00:00Z"",
    ""PatternDate"":""2012-11-07T00:00:00Z""
}", settings);
var value = data["SimpleDate"].Value<string>();
Debug.WriteLine(value); // Outputs 2012-05-18T00:00:00Z
JObject.Parse() 需要 JsonSerializerSettings,因此请使用 反序列化对象.此设置最终会传播到 JsonReader.DateParseHandling一个>.
There's no overload to JObject.Parse() that takes a JsonSerializerSettings, so use DeserializeObject.  This setting eventually gets propagated to JsonReader.DateParseHandling.
Newtonsoft 相关文档:
Related Newtonsoft docs:
- Json.NET 在反序列化为 JObject 时解释和修改 ISO 日期#862
 - 在 JSON 中序列化日期.
 
这篇关于JToken:获取原始/原始 JSON 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JToken:获取原始/原始 JSON 值
				
        
 
            
        - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 - 使用 rss + c# 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 
						
						
						
						
						