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 值
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01