This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19254|Reply: 2

[Source] Use Newtonsoft.Json in .NET to convert, read, write json

[Copy link]
Posted on 12/14/2015 5:33:50 PM | | |

First of all, you need to understand what JSON is, you can click https://www.ibm.com/developerworks/cn/web/wa-lo-json/ to learn more about JSON, I will briefly introduce JSON here:
    JSON stands for Javascrip{filter}t Object Natation, which is a lightweight data exchange format that is ideal for server interaction with Javascrip{filter}t. Like XML, JSON is a plain text-based data format. Since JSON is inherently prepared for Javascrip{filtering}t, the data format of JSON is very simple, you can transfer a simple String, Number, Boolean, an array, or a complex Object object in JSON.
     In the .NET environment, we use Json.net to serialize and deserialize JSON data.
     Start by clicking Connect http://json.codeplex.com/ to download the JSON. .NET plugins and code.
     Then make a reference Newtonsoft.Json.dll in your project
     Add namespace: using Newtonsoft.Json;
     The following are some important methods and examples of JSON serialization and deserialization:
JsonConvert.SerializeObject(object value), which has an overload method JsonConvert.SerializeObject(object value, params JsonConverter[] converters).
JsonConvert.DeserializeObject(string value, Type type), deserialized, it has an overload method JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
These two methods can achieve basic serialization and deserialization requirements, see the following examples:
First, let's 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) Serialization
using 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:
{"Name":"GoldenEasy","Age":25}
2) deserialization
using 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: GoldenEasy




Previous:【iOS Development Series Tutorial Released in the Summer】iPhone Tutorial
Next:Thread multithreading The important role of IsBackground for threads
 Landlord| Posted on 1/13/2016 4:02:50 PM |
Send the .net2.0 to .net4.5 versions

Bin.rar (1.71 MB, Number of downloads: 2)
Posted on 5/18/2016 5:53:51 PM |
Deserialization can also be written like this: List<Model.WebCMS.FloorClass> floor = JsonConvert.DeserializeObject<List<Model.WebCMS.FloorClass>>(html);
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com