C ා access JSON data directly through anonymous classes

February 9, 2018, yushimen.

Experimental purpose

In the process of interaction between C ා and Rest/Restful and other Web services, a lot of data is transferred to JSON. How to transform C ා object to JSON and JSON to C ා object quickly becomes the content frequently used in the development process. In the traditional way, we need to define C ා classes that are synchronized with JSON objects and instantiate them into objects for operations. Here we provide a way to directly use anonymous classes. Compared with the traditional way, we can define fewer classes and operate more easily.

development environment

2018-02-09-15-22-23.png

Implementation steps

1. Create project: console program, named JsonTest

2018-02-09-15-29-43.png

2. Add component: add JSON component

Step 1: right click on the item and choose "manage NuGet package" menu

2018-02-09-15-32-31.png

Step 2: in the "NuGet: JsonTest" tab, select browse, enter JSON, press enter, select Newtonsoft.Json, select version, and click Install.

2018-02-09-15-35-12.png

Step 3: complete the installation

2018-02-09-15-37-13.png

3. Write code: simple structure

JSON format is as follows:

{
    "name":"Zhang San",
    "sex":"male",
    "birthday": "2018-02-09"
}

CSharp code

using Newtonsoft.Json;
using System;

namespace JsonTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonstr = JsonConvert.SerializeObject(new
            {
                name = "Zhang San",
                sex = "male",
                birthday = "2018-02-09"
            });
            Console.WriteLine("The serialized string of the object is:");
            Console.WriteLine(jsonstr);

            Console.WriteLine("\n\n The value after deserializing a string to an object is:");
            var jsonobj = JsonConvert.DeserializeObject<dynamic>(jsonstr);

            Console.WriteLine("Full name:" + jsonobj.name);
            Console.WriteLine("Gender:" + jsonobj.sex);
            Console.WriteLine("Birthday:" + jsonobj.birthday);
            Console.Read();
        }
    }
}

results of enforcement

2018-02-09-15-46-47.png

4. Write code: array operation

CSharp code

using Newtonsoft.Json;
using System;

namespace JsonTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string jsonstr = JsonConvert.SerializeObject(new[]{
                new {
                    name = "Zhang San",
                    sex = "male",
                    birthday = "2018-02-09"
                },
                new{
                    name = "Li Si",
                    sex = "male",
                    birthday = "2018-02-09"
                }
            });
            Console.WriteLine("The serialized string of the object is:");
            Console.WriteLine(jsonstr);

            Console.WriteLine("\n\n The value after deserializing a string to an object is:");
            var jsonarr = JsonConvert.DeserializeObject<dynamic[]>(jsonstr);
            foreach (var jsonobj in jsonarr)
            {
                Console.WriteLine("Full name:" + jsonobj.name);
                Console.WriteLine("Gender:" + jsonobj.sex);
                Console.WriteLine("Birthday:" + jsonobj.birthday);
                Console.WriteLine("---------------");
            }
            Console.Read();
        }
    }
}

results of enforcement

2018-02-09-15-55-34.png

Tags: JSON REST

Posted on Tue, 07 Apr 2020 10:04:37 -0700 by padma