我有一些包含注释的JSON(尽管在JSON规范中严格禁止使用注释)。我如何使用System.Text.Json
解析此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)
System.Text.Json
解析或反序列化这个 JSON?