C#实体类转换为JSON字符串

C#实体类转换为JSON

使用程序集:Newtonsoft.Json



代码

不说二话,直接上代码

using System;
// @author Simorel
// https://blog.csdn.net/Simoral 
// 引用程序集: Newtonsoft.Json.dll
using Newtonsoft.Json;

namespace 可删除_随便删
{
    /// <summary>
    /// 测试类
    /// </summary>
    public class Test
    {
        public string TestA { get; set; }  // 测试参数A
        public string TestB { get; set; }  // 测试参数B
        public string TestC { get; set; }  // 测试参数C
    }

    class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            test.TestA = "A";
            test.TestB = "B";
            test.TestC = "C";

            // 实际使用转换用代码,test是实体类对象
            string strJson = JsonConvert.SerializeObject(test);

            Console.WriteLine(strJson);
            Console.ReadKey();
        }
    }    
}

关键

还有记得添加引用

添加引用:using Newtonsoft.Json;

关键代码: JsonConvert.SerializeObject(obj)

其中obj是实体类对象

程序集

程序集:Newtonsoft.Json.dll
这个只是提供一个帮助 [手动脸红][手动脸红][手动脸红],如果您没有资源分,请百度这个Newtonsoft.Json就行

猜你喜欢

转载自blog.csdn.net/Simoral/article/details/80625654
今日推荐