Use Newtonsoft.json to convert, read, write JSON in. Net

Source: Internet
Author: User

First of all, we have to understand what is JSON, to learn more about the JSON aspects of the information you can click Https://www.ibm.com/developerworks/cn/web/wa-lo-json/, I am here to briefly introduce the next Json:json that JavaScript Object Natation, a lightweight data interchange format, is ideal for server-to-JavaScript interaction. As with XML, JSON is also a data format based on plain text.     Since JSON is inherently prepared for JavaScript, the JSON data format is very simple, you can transfer a simple string,number,boolean with JSON, or you can transfer an array, or a complex object.     Under the. NET environment, we use json.net to serialize and deserialize JSON data. First click Connect HTTP://SOURCEFORGE.NET/PROJECTS/CSJSON/?SOURCE=DLP to download the JSON.     NET plug-ins and code.     Then make a reference in the project Newtonsoft.Json.dll add namespace: Using Newtonsoft.json; Here are some important methods and examples of JSON serialization and deserialization:Jsonconvert.serializeobject (object value)serialization, it has an overloaded methodJsonconvert.serializeobject (object value, params jsonconverter[] converters).
Jsonconvert.deserializeobject (String value, type type),Deserialization, it has an overloaded methodJsonconvert.deserializeobject (String value, type type, params jsonconverter[] converters)
These two methods can implement basic serialization and deserialization requirements, see the following example: First, we first build a person class code as follows: public class person
{
private string name;
public string Name
{
get {return name;}
set {name = value;}
} private int age;
public int Age
{
get {return age;}
set {age = value;}
}
}1) SerializationUsing System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Newtonsoft.json; namespace jsonnet
{
    public partial class test: System.Web.UI.Page
    {
        protected void Page_Load (object sender, EventArgs e)
        {
             person person = new person ();
            person. Name = "Goldeneasy";
            person. Age = 25;
            string StrSerializeJSON = Jsonconvert.serializeobject (person);
            response.write (StrSerializeJSON);                        }
}
}Output Result:{"Name": "Goldeneasy", "Age": 25}2) DeserializationUsing System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Newtonsoft.json; Namespace Jsonnet
{
public partial class Test:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
person person = new person ();
Person. Name = "Goldeneasy";
Person. Age = 25;
String Strserializejson = Jsonconvert.serializeobject (person);
Person user = (person) jsonconvert.deserializeobject (Strserializejson, typeof (person));
Response.Write (user. Name);
}
}
The output result is: goldeneasyhttp://blog.sina.com.cn/s/blog_70686f3a0101kemg.html

Use Newtonsoft.json to convert, read, write JSON in. Net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.