Json Class
Provides methods for working with data in JavaScript Object Notation (JSON) format.
Inheritance Hierarchy
System.Object
System.Web.Helpers.Json
Namespace: System.Web.Helpers
Assembly: System.Web.Helpers (in System.Web.Helpers.dll)
Syntax
'Declaration
Public NotInheritable Class Json
'Usage
public static class Json
public ref class Json abstract sealed
[<AbstractClassAttribute>]
[<SealedAttribute>]
type Json = class end
public final class Json
The Json type exposes the following members.
Methods
Name | Description | |
---|---|---|
Decode(String) | Converts data in JavaScript Object Notation (JSON) format into a data object. | |
Decode(String, Type) | Converts data in JavaScript Object Notation (JSON) format into a data object of a specified type. | |
Decode<T>(String) | Converts data in JavaScript Object Notation (JSON) format into the specified strongly typed data list. | |
Encode | Converts a data object to a string that is in the JavaScript Object Notation (JSON) format. | |
Write | Converts a data object to a string in JavaScript Object Notation (JSON) format and adds the string to the specified TextWriter object. |
Top
Remarks
This class represents a helper, which is a component that simplifies web programming in ASP.NET Web Pages. You can use the Json class to encode or decode data in JSON format.
HTTP requires structured data to be converted, or serialized, to a format that can be read across the web by different processes. Popular serialization formats are JSON and XML, which are both are text-based formats. A typical use for the JSON format is to retrieve data from a database, a web service, or other sources, encode it in JSON format, and then return it to a requesting browser as part of a web page.
For example, after you retrieve data using a query in ASP.NET Razor code, you can call Encode(Object) to encode the data from the query and assign the results to a JavaScript variable in a client code block. In the browser, after you get the web page that contains the JSON data, you can use JavaScript code to manipulate how the data is displayed in the browser without sending additional requests to the server. You can also provide controls and code that enable users to update the data and then post it to a web server.
In ASP.NET and Razor pages, you typically use the JavaScript-based jQuery or ASP.NET Ajax libraries for client-side data manipulation. These libraries provide an extensive infrastructure that helps you build pages that perform client-side data manipulation using JavaScript, with minimal round trips to a server.
When users have finished viewing the page that contains the data, they can submit the page. In the server-side page code, when you receive posted JSON data, you can use the Decode(String) method to convert the data from JSON format into a data object. You can then use code (for example, a foreach loop) to process the posted data and perform tasks such as updating a database, displaying the updated data on the page, or serving the data to other requests by using a web service.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.