C # Newtonsoft.Json json string parsing process (the most clear and understandable way)

demand:

Suppose the following character string json:

{
    "companyID": "15",
 
    "employees": [
        {
            "firstName": "Bill",
            "lastName": "Gates"
        },
        {
            "firstName": "George",
            "lastName": "Bush"
        }
    ],
 
    "manager": [
        {
            "salary": "6000",
            "age": "23"
        },
        {
            "salary": "8000",
            "age": "26"
        }
    ]
 
}

 

 

Please use C # to deal with this string, the console shows the company's ID, name, and salaries for all managers of the employee.

Solving steps:

(For clarity of description, in this embodiment the test console, I programming environment VS2015)

1, download the open source library Newtonsoft.Json (Download http://json.codeplex.com/).

2, the new project in VS - console application.

3, reference library: Click VS: Project - References - browse - find you just downloaded Newtonsoft.Json.dll-- OK.

4, at the beginning of the main program Program.cs added using Newtonsoft.Json; this statement.

5, the copy json string to be processed to obtain a C # class http://json2csharp.chahuo.com/, as shown:

Part of this copy and paste it into the lower class Program class.

6, for the convenience of presentation, I gave the assignment to a variable json string. But there are a lot of quotes in json, assigned to a string variable plus a lot of backslash. Here we give an online tool: http: //tool.chinaz.com/tools/jsonescape.aspx

Copy the things I have to go click on the "compression and escape", the resulting value can be assigned directly to a string variable, as shown:

PS: if from somewhere else such as webAPI get json string that better, directly assigned to the following operations after a string variable, eliminating the need to escape this step.

string jsonText = " {\"companyID\":\"15\",\"employees\":[{\"firstName\":\"Bill\",\"lastName\":\"Gates\"},{\"firstName\":\"George\",\"lastName\":\"Bush\"}],\"manager\":[{\"salary\":\"6000\",\"age\":\"23\"},{\"salary\":\"8000\",\"age\":\"26\"}]}  ";  

7, obtained deserialization json string object.

RootObject rb = JsonConvert.DeserializeObject<RootObject>(jsonText);

8, the processing object, such as output company ID:

Console.WriteLine(rb.companyID);

Complete code as follows (reference can be run directly after NewtonSoft.Json.dll):

using Newtonsoft.Json;  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace ConsoleJsonTest  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
  
            string jsonText = " {\"companyID\":\"15\",\"employees\":[{\"firstName\":\"Bill\",\"lastName\":\"Gates\"},{\"firstName\":\"George\",\"lastName\":\"Bush\"}],\"manager\":[{\"salary\":\"6000\",\"age\":\"23\"},{\"salary\":\"8000\",\"age\":\"26\"}]} ";  
            Console.WriteLine(jsonText);  
  
            RootObject rb = JsonConvert.DeserializeObject<RootObject>(jsonText);  
  
            Console.WriteLine(rb.companyID);  
  
            Console.WriteLine(rb.employees[0].firstName);  
  
            foreach (Manager ep in rb.manager)  
            {  
                Console.WriteLine(ep.age);  
            }  
  
            Console.ReadKey();  
        }  
    }  
  
    public class Employees  
    {  
        public string firstName { get; set; }  
        public string lastName { get; set; }  
    }  
  
    public class Manager  
    {  
        public string salary { get; set; }  
        public string age { get; set; }  
    }  
  
    public class RootObject  
    {  
        public string companyID { get; set; }  
        public List<Employees> employees { get; set; }  
        public List<Manager> manager { get; set; }  
    }  
}  

 

Output:

 

Note: For json string array, as in this example or employees Manager, with this article online tool automatically generates a generic list List <T>. About generic lists of related content in this article will not go into details, you can directly use it as an array. Iterate the methods are still applicable, for example, code:

foreach (Manager ep in rb.manager)
 
{
 
Console.WriteLine(ep.age);
 
}

 

Guess you like

Origin www.cnblogs.com/zouhao/p/11420072.html