Hướng dẫn dùng jsondocument JavaScript
Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. How to use a JSON document, Utf8JsonReader, and Utf8JsonWriter in System.Text.Json
In this articleThis article shows how to use:
JSON DOM choicesWorking with a DOM is an alternative to deserialization with JsonSerializer:
Consider the following factors when choosing between
Use JsonNodeThe following example shows how to use JsonNode and the other types in the System.Text.Json.Nodes namespace to:
Create a JsonNode DOM with object initializers and make changesThe following example shows how to:
Deserialize subsections of a JSON payloadThe following example shows how to use JsonNode to navigate to a subsection of a JSON tree and deserialize a single value, a custom type, or an array from that subsection.
JsonNode average grade exampleThe following example selects a JSON array that has integer values and calculates an average value:
The preceding code:
JsonNode with JsonSerializerOptionsYou can use The same limitation applies to the The following example illustrates the result of using methods that take a
If you need features of
Use JsonDocumentThe following example shows how to use the JsonDocument class for random access to data in a JSON string:
The preceding code:
The preceding example code counts students by incrementing a
Here's an example of the JSON that this code processes:
How to search a JsonDocument and JsonElement for sub-elementsSearches on
Use JsonDocument to write JSONThe following example shows how to write JSON from a JsonDocument:
The preceding code:
Here's an example of JSON input to be processed by the example code:
The result is the following pretty-printed JSON output:
JsonDocument is IDisposable
Only return a Here's an example that requires you to make a
The preceding code expects a If you receive a
JsonDocument with JsonSerializerOptionsYou can use The following example illustrates the result of using methods that take a
If you need features of Use Utf8JsonWriterUtf8JsonWriter is a high-performance way to write UTF-8 encoded JSON text from common .NET types like The following example shows how to use the Utf8JsonWriter class:
Write with UTF-8 textTo
achieve the best possible performance while using the This approach also works if you need to do custom escaping. Write raw JSONIn some scenarios, you might want to write "raw" JSON to a JSON payload that you're creating with
Customize character escapingThe StringEscapeHandling setting of Write null valuesTo write null values by using
For a string property, if the string is null, WriteString and
WriteStringValue are equivalent to Write Timespan, Uri, or char valuesTo write Use Utf8JsonReaderUtf8JsonReader is a high-performance, low allocation, forward-only reader for UTF-8 encoded JSON text, read from a The following example shows how to use the Utf8JsonReader class:
The preceding code assumes that the Filter data using Utf8JsonReaderThe following example shows how to synchronously read a file, and search for a value.
For an asynchronous version of this example, see .NET samples JSON project. The preceding code:
Here's a JSON sample that the preceding code can read. The resulting summary message is "2 out of 4 have names that end with 'University'":
Read from a stream using Utf8JsonReaderWhen reading a large file (a gigabyte or more in size, for example), you might want to avoid having to load the entire file into memory at once. For this scenario, you can use a FileStream. When using the
The following code illustrates how to read from a stream. The example shows a MemoryStream. Similar code will work with a
FileStream, except when the The sample code starts with a 4KB buffer and doubles the buffer size each
time it finds that the size is not large enough to fit a complete JSON token, which is required for the reader to make forward progress on the JSON payload. The JSON sample provided in the snippet triggers a buffer size increase only if you set a very small initial buffer size, for example, 10 bytes. If you set the initial buffer size to 10, the
The preceding example sets no limit to how large the buffer can grow. If the token size is too large, the code could fail with an OutOfMemoryException exception. This can happen if the JSON contains a token that is around 1 GB or more in size, because doubling the 1 GB
size results in a size that is too large to fit into an Utf8JsonReader is a ref structBecause the Read UTF-8 textTo achieve the best possible performance while using the Read with multi-segment ReadOnlySequenceIf your JSON input is a
ReadOnlySpan
Use ValueTextEquals for property name lookupsDon't use ValueSpan to do byte-by-byte comparisons by calling SequenceEqual for property name lookups. Call ValueTextEquals instead, because that method unescapes any characters that are escaped in the JSON. Here's an example that shows how to search for a property that is named "name":
Read null values into nullable value typesThe built-in
See also
FeedbackSubmit and view feedback for |