gpt4 book ai didi

c# - Json.NET 中的 JConstructor 和 JRaw

转载 作者:行者123 更新时间:2023-11-30 15:21:43 24 4
gpt4 key购买 nike

根据 this StackOverflow 上的回答:

Json.NET includes many features which are not part of JSON specification. In particular, it allows parsing some JSON files which are "officially" invalid. This includes unquoted properties, comments, constructors etc.

这些是可从JToken 分配的所有类型:

JArray
JConstructor
JContainer
JObject
JProperty
JRaw
JValue

请判断下列情况是否正确:

  1. “官方”有效的 json 上的 JToken.Parse(json) 不可能在其后代中包含 JConstructorJRaw.

  2. 如果 json 是“正式”有效的,那么在这些后代中只能看到以下类型:JArrayJObject JProperty, JValue.

最佳答案

你的陈述是真实的。

  1. JConstructor设计用于捕获 JavaScript Date format 中的日期,例如:new Date(1234656000000)。如 Serializing Dates in JSON 中所述:

    Technically this is invalid JSON according to the spec, but all browsers and some JSON frameworks, including Json.NET, support it.

    因此在解析严格符合 current IETF proposed standard 的 JSON 时,不会出现 JConstructororiginal JSON proposal .

JRaw使用 JToken.Parse(string) 解析 JSON 时永远不会出现.它主要用于促进从 JToken 层次结构中编写预格式化的 JSON 文字。通过使用 JRaw,可以避免仅仅为了发出它而解析已经格式化的 JSON,例如:

        var root = new JObject(new JProperty("response", new JRaw(jsonLiteral)));
var rootJson = root.ToString();

can be done instead of the less-efficient:

var root = new JObject(new JProperty("response", JToken.Parse(jsonLiteral)));

It's also possible to deserialize to `JRaw` to capture a JSON hierarchy as a single string literal, though I don't see much use in doing so. For instance, given the class:

public class RootObject
{
public JRaw response { get; set; }
}

One can do:<p>

var rootDeserialized = JsonConvert.DeserializeObject<RootObject>(rootJson);
var jsonLiteralDeserialized = (string)rootDeserialized.response;

However, this is not necessarily more efficient than deserializing to a `JToken`.
  1. 如您所料,在解析严格有效的 JSON 时,只有 JArrayJObjectJPropertyJValue 会出现.

关于c# - Json.NET 中的 JConstructor 和 JRaw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com