C # Operations JSON

Source: Internet
Author: User

(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

    1. Steps
    2. Understanding the syntax of JSON
    3. Examples of creating entity classes
    4. Entity classes that automatically generate JSON
    5. 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

Related Article

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.