问题描述
我正在将newtonsoft实现转换为.net core 3.0中的新JSON库.我有以下代码
I am converting my newtonsoft implementation to new JSON library in .net core 3.0. I have the following code
public static bool IsValidJson(string json)
{
try
{
JObject.Parse(json);
return true;
}
catch (Exception ex)
{
Logger.ErrorFormat("Invalid Json Received {0}", json);
Logger.Fatal(ex.Message);
return false;
}
}
我找不到JObject.Parse(json);
JsonProperty
等价 >
public class ResponseJson
{
[JsonProperty(PropertyName = "status")]
public bool Status { get; set; }
[JsonProperty(PropertyName = "message")]
public string Message { get; set; }
[JsonProperty(PropertyName = "Log_id")]
public string LogId { get; set; }
[JsonProperty(PropertyName = "Log_status")]
public string LogStatus { get; set; }
public string FailureReason { get; set; }
}
我将要寻找的另一件事是Formating.None
.
One more thing i will be looking for the equivalent of Formating.None
.
推荐答案
您在这里问了几个问题:
You are asking a few questions here:
-
我找不到
JObject.Parse(json);
您可以使用 JsonDocument
到解析并检查任何JSON,以其 RootElement
.根元素的类型为 JsonElement
,表示任何JSON值(是否为原始值),并对应于Newtonsoft的 JToken
.
You can use JsonDocument
to parse and examine any JSON, starting with its RootElement
. The root element is of type JsonElement
which represents any JSON value (primitive or not) and corresponds to Newtonsoft's JToken
.
但是请务必注意此文档备注:
But do take note of this documentation remark:
当您需要使用 JsonElement
在其文档生命周期之外,您必须克隆它:
还请注意,JsonDocument
当前为只读,并且不提供用于创建或修改JSON的API. 问题#39922:可写Json DOM 对此进行跟踪.
Also note that JsonDocument
is currently read-only and does not provide an API for creating or modifying JSON. There is an open issue Issue #39922: Writable Json DOM tracking this.
使用示例如下:
//https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#using-declarations
using var doc = JsonDocument.Parse(json);
//Print the property names.
var names = doc.RootElement.EnumerateObject().Select(p => p.Name);
Console.WriteLine("Property names: {0}", string.Join(",", names)); // Property names: status,message,Log_id,Log_status,FailureReason
//Re-serialize with indentation.
using var ms = new MemoryStream();
using (var writer = new Utf8JsonWriter(ms, new JsonWriterOptions { Indented = true }))
{
doc.WriteTo(writer);
}
var json2 = Encoding.UTF8.GetString(ms.GetBuffer(), 0, checked((int)ms.Length));
Console.WriteLine(json2);
JsonProperty
等效属性是什么?
Also what will be the attribute JsonProperty
equivalent?
可以控制 JsonSerializer
放在 System.Text.Json.Serialization
命名空间并从抽象基类继承 JsonAttribute
.与JsonProperty
不同,没有可以控制属性序列化所有方面的综合属性.而是有特定的属性来控制特定的方面.
Attributes that can control JsonSerializer
are placed in the System.Text.Json.Serialization
namespace and inherit from an abstract base class JsonAttribute
. Unlike JsonProperty
, there is no omnibus attribute that can control all aspects of property serialization. Instead there are specific attributes to control specific aspects.
从.NET Core 3开始,这些功能包括:
As of .NET Core 3 these include:
这是您要用来控制ResponseJson
类的序列化名称的属性:
This is attribute you want to use to control the serialized names of your ResponseJson
class:
public class ResponseJson
{
[JsonPropertyName("status")]
public bool Status { get; set; }
[JsonPropertyName("message")]
public string Message { get; set; }
[JsonPropertyName("Log_id")]
public string LogId { get; set; }
[JsonPropertyName("Log_status")]
public string LogStatus { get; set; }
public string FailureReason { get; set; }
}
[JsonConverterAttribute(Type)]
:
请注意,转换器的优先级-属性的属性,然后是Converters集合的选项,然后是类型的属性-与记录的顺序不同用于 Newtonsoft转换器,它是 JsonConverter定义的通过成员上的属性,然后是通过类上的属性定义的JsonConverter,最后是传递给JsonSerializer的所有转换器.
Note that the documented priority of converters -- Attribute on property, then the Converters collection in options, then the Attribute on type -- differs from the documented order for Newtonsoft converters, which is the JsonConverter defined by attribute on a member, then the JsonConverter defined by an attribute on a class, and finally any converters passed to the JsonSerializer.
[JsonExtensionDataAttribute]
-对应于Newtonsoft的 [JsonExtensionData]
.
[JsonIgnoreAttribute]
-对应于Newtonsoft的 [JsonIgnore]
.
通过 ,可以通过设置 JsonWriterOptions.Indented
到true
或false
.
When writing JSON via Utf8JsonWriter
, indentation can be controlled by setting JsonWriterOptions.Indented
to true
or false
.
通过 ,可以通过设置 JsonSerializerOptions.WriteIndented
到true
或false
.
When serializing to JSON via JsonSerializer.Serialize
, indentation can be controlled by setting JsonSerializerOptions.WriteIndented
to true
or false
.
演示小提琴此处显示了JsonSerializer
的序列化和JsonDocument
的解析.
Demo fiddle here showing serialization with JsonSerializer
and parsing with JsonDocument
.
这篇关于在.net core 3中将newtonsoft代码转换为System.Text.Json.等效于JObject.Parse和JsonProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!