(Turn from: Small tank http://www.cnblogs.com/TankXiao/p/3637495.html)
In Restapi, it is often necessary to manipulate JSON strings, to "deserialize" the JSON string into an object, and to "serialize" an object into a string.
C # Operations JSON, relatively simple. This article describes several methods
Read Catalogue
- Steps
- Understanding the syntax of JSON
- Examples of creating entity classes
- Entity classes that automatically generate JSON
- Using third-party tools Newtonsoft.Json.dll
Steps
A total of 2 steps are required to deserialize a Json string into an object:
First step: Establish the corresponding entity class
Step Two: Call the method to "deserialize" the JSON string into an object
Understanding the syntax of JSON
The first thing to learn about JSON syntax is to know how to create an entity class.
The syntax of Json is very simple, just four lines
- Data in name/value pairs
- Data is separated by commas
- Curly braces Save Object
- Square brackets Save Array
Examples of creating entity classes
{"FirstName":" small tank ","LastName":"Tank","Age": (+}
The corresponding entity class can be considered
Class person {public String FirstName setpublicsetintset;}}
Entity classes that automatically generate JSON
A complex entity class, we can write it ourselves, it may take more than 1 hours. Is there any tool that can generate the entity class directly, so it's much more convenient. Of course
http://jsonclassgenerator.codeplex.com/----Open source WinForm tools.
Using third-party tools Newtonsoft.Json.dll
Advantages: Very convenient to use. Look at an example and know how to use it.
Http://james.newtonking.com/json
code example:
UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingNewtonsoft.json;Namespacejsondemo{Classprogram {Staticvoid Main (String[] args) {String Personjson ="{' FirstName ': ' Small Tank ', ' LastName ': ' Tank Xiao ', ' age ': ' + ', ' Books ': [{' BookName ': ' C # ', ' Price ': ' 29.9 '},{' bookname ': ' Mac programming ', ' price ': ' 39.9 '}]}";String Allmovejson =@"[{' FirstName ': ' Small Tank ', ' LastName ': ' Tank Xiao ', ' age ': ' + ', ' Books ': [{' BookName ': ' C # ', ' Price ': ' 29.9 '},{' bookname ': ' Mac programming ', ' price ': ' 39.9 '}]},{' FirstName ': ' Small tank 2 ', ' LastName ': ' Tank xiao2 ', ' age ': ' + ', ' Books ': [{' BookName ': ' C # ', ' Price ': ' 29.9 '},{' bookname ': ' Mac programming ', ' price ': ' 39.9 '}]}";//Deserialize a single object person Onemovie = jsonconvert.deserializeobject<person>(Personjson);//Deserializes a collection of objects list<person> Allmovie = jsonconvert.deserializeobject<list<person>>(Allmovejson); Console.WriteLine (Onemovie.firstname); Console.WriteLine (allmovie[1]. FirstName);//Serialization ofString Afterjson =Jsonconvert.serializeobject (Allmovie); } }PublicClassperson {PublicString FirstName {GetSet; }PublicString LastName {GetSet; }PublicIntAge {Getsetpublic list<book> Books {get; setpublic class book { Span style= "color: #0000ff;" >public string BookName { get; setpublic string price { Span style= "color: #0000ff;" >get; set
Attached: C # Usage tips (in serial, please expect)
C # Tips (1) C # conversion timestamp
C # Tips (2) C # operations JSON
C # Operations JSON