I have some JSON that includes comments (even though comments aren't strictly allowed in the JSON spec.) How can I parse this JSON using System.Text.Json
?
The JSON I have received is as folows:
// A person
{
"Id" : 1 /* Person's ID */,
"Name" : "Foo" // Person's name
}
When I attempt to load it into a JsonDocument
like so:
using var doc = JsonDocument.Parse(jsonString);
I get the following exception:
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)```
And when I attempt to deserialize with JsonSerializer
:
var person = JsonSerializer.Deserialize<Person>(jsonString);
I get a similar exception:
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)
How can I parse or deserialize this JSON with System.Text.Json
?