我有一些包含注释的 JSON(即使JSON 规范中不严格允许注释。)如何使用 解析这个 JSON System.Text.Json
?
我收到的 JSON 如下:
// A person
{
"Id" : 1 /* Person's ID */,
"Name" : "Foo" // Person's name
}
当我尝试将它加载到JsonDocument
这样的:
using var doc = JsonDocument.Parse(jsonString);
我得到以下异常:
System.Text.Json.JsonReaderException: '/' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0. at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes) at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)```
当我尝试反序列化时JsonSerializer
:
var person = JsonSerializer.Deserialize<Person>(jsonString);
我得到一个类似的例外:
System.Text.Json.JsonException: '/' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0. ---> System.Text.Json.JsonReaderException: '/' is an invalid start of a value. LineNumber: 0 | BytePositionInLine: 0. at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes) at System.Text.Json.Utf8JsonReader.ConsumeValue(Byte marker)
如何使用 解析或反序列化此 JSON System.Text.Json
?