【发布时间】:2020-07-22 14:55:15
【问题描述】:
public void Set(string name, object content)
{
this.Json.Add(name, content);
}
如何将内容变量转换为 Jtoken 以便执行 Json.add 操作?
【问题讨论】:
public void Set(string name, object content)
{
this.Json.Add(name, content);
}
如何将内容变量转换为 Jtoken 以便执行 Json.add 操作?
【问题讨论】:
你可以使用JObject.FromObject:
this.Json.Add(name, JObject.FromObject(content));
或JValue.FromObject,如果您期望的值未映射到 json 对象:
this.Json.Add(name, JValue.FromObject(content));
【讨论】:
JValue.FromObject(content)。
JValue.FromObject(),您实际上是在调用基类JToken 上的静态方法。请参阅:JSON.NET: Why Use JToken--ever?。