Home>

In RESTAPI, you often need to manipulate the JSON string. You need to "deserialize" the JSON string into an object. You also need to "serialize" an object into a string.

c#operation json, relatively simple. This article introduces several methods

step

Deserializing a json string into an object requires two steps:

Step 1:Create the corresponding entity class

Step 2:Call the method to "deserialize" the json string into an object

Understand the syntax of json

First understand the syntax of json so that you know how to create entity classes.

The syntax of json is very simple, Just four

Data in name/value pairs

Data is separated by commas

Curly braces save objects

Square brackets hold array

Example of creating entity class

{"firstname":"Little tank", "lastname":"tank", "age":30}

The corresponding entity class can be

 public class person
  {
    public string firstname
    {get;set;}
    public string lastname
    {get;set;}
    public int age
    {get;set;}
  }

Automatically generate json entity classes

A complex entity class,If we write it ourselves,It may take more than an hour. Are there any tools that can directly generate entity classes?This can be too convenient. Of course there is

---- Open source winform tool.

Use third-party tool newtonsoft.json.dll

Pros:Very easy to use. Look at an example to know how to use

Code example:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using newtonsoft.json;
namespace jsondemo
{
  class program
  {
    static void main (string [] args)
    {
      string personjson="{" firstname ":" small tank "," lastname ":" tank xiao "," age ":" 30 "," books ":[{" bookname ":" c#"," price ":" 29.9 "}, {" bookname ":" mac programming "," price ":" 39.9 "}]}";
      string allmovejson [email protected]"[{" firstname ":" small tank "," lastname ":" tank xiao "," age ":" 30 "," books ":[{" bookname ":" c#"," price " :"29.9"}, {"bookname":"mac programming", "price":"39.9"}]}, {
               "firstname":"Little Tank 2", "lastname":"tank xiao2", "age":"40", "books":[{"bookname":"c#", "price":"29.9"}, {"bookname":"mac programming", "price":"39.9"}]}] ";
      //deserialize a single object
      person onemovie=jsonconvert.deserializeobject<person>(personjson);
      //deserialize object collection
      list<person>allmovie=jsonconvert.deserializeobject<list<person>
      console.writeline (onemovie.firstname);
      console.writeline (allmovie [1] .firstname);
      //Serialization
      string afterjson=jsonconvert.serializeobject (allmovie);
    }
  }
  public class person
  {
    public string firstname
    {get;set;}
    public string lastname
    {get;set;}
    public int age
    {get;set;}
    public list<book>books
    {get;set;}
  }
  public class book
  {
    public string bookname
    {get;set;}
    public string price
    {get;set;}
  }
}

Finally we look at an example

using system;
using system.collections.generic;
using system.text;
using newtonsoft.json;
namespace testjson
{
  class program
  {
    static void main (string [] args)
    {
      string json="{\" a \ ":\" sadsad \ ", \" c \ ":{\" d \ ":\" adad \ "}}";
      //ccc ccc=new ccc ();
      //ccc.d="adad";
      //aaa aaa=new aaa ();
      //aaa.a="sadsad";
      //aaa.b="dfgdg";
      //aaa.c=ccc;
      aaa aaa=new aaa ();
      aaa=javascriptconvert.deserializeobject<aaa>(json);
      //string json=javascriptconvert.serializeobject (aaa);
      //console.writeline (json);
      console.read ();
    }
  }
  public class aaa {
    private string m_a;
    public string a
    {
      get {return m_a;}
      set {m_a=value;}
    }
    private string m_b;
    public string b
    {
      get {return m_b;}
      set {m_b=value;}
    }
    private ccc m_c;
    public ccc c
    {
      get {return m_c;}
      set {m_c=value;}
    }
  }
  public class ccc
  {
    private string m_d;
    public string d
    {
      get {return m_d;}
      set {m_d=value;}
    }
  }
}
c
  • Previous Detailed drawing methods using Java's Graphics class
  • Next The hover method in jQuery with the css hover selector to achieve the highlighted element selection method