C#使用Newtonsoft库序列化不同容器、实体后的字符串格式

tech2026-02-16  0

一、实现效果

1.1、List容器对应的序列化效果

①List<string> 类型序列化后的Json字符串:

②List<TestEntity> 类型序列化后的Json字符串:

③List<Dictionary<string, string>> 类型序列化后的Json字符串:

注意:TestEntity是一个实体类Unity版的Newtonsoft库下载

1.2、Dictionary容器对应的序列化效果

①Dictionary<string, string> 类型序列化后的Json字符串:

②Dictionary<string, TestEntity> 类型序列化后的Json字符串:

③Dictionary<string, List<string>> 类型序列化后的Json字符串:

④Dictionary<string, List<TestEntity>> 类型序列化后的Json字符串:

1.3、实体对应的序列化效果

TestEntity类型序列化后的Json字符串:

二、实现思路

2.1、引用Newtonsoft库进行序列化为json字符串

2.2、创建实现List容器序列化的Json字符串的方法

2.3、创建实现Dictionary容器序列化的Json字符串的方法

2.4、创建实现实体类序列化的Json字符串的方法

三、实现方法

/*** * Title:"三维可视化" 项目 * 主题:【测试层】 * Description: * 功能: * 1、序列化不同容器内容为Json字符串 * Date:2020 * Version:0.1版本 * Author:Coffee * Modify Recoder: */ using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace TestFunction { public class Test_Json : MonoBehaviour { #region 基础参数 List<string> _List = new List<string>(); List<TestEntity>_ListTestEntities = new List<TestEntity>(); List<Dictionary<string, string>> _LisDic = new List<Dictionary<string, string>>(); Dictionary<string, string> _Dic = new Dictionary<string, string>(); Dictionary<string, TestEntity> _DicEntities = new Dictionary<string, TestEntity>(); Dictionary<string, List<string>> _DicListStr = new Dictionary<string, List<string>>(); Dictionary<string, List<TestEntity>> _DicListEntities = new Dictionary<string, List<TestEntity>>(); TestEntity _TestEntity = new TestEntity(); #endregion #region Unity自带方法 void Start() { //初始化数据 InitData(); } void Update() { if (Input.GetKeyDown(KeyCode.P)) { //列表系列化后的字符串 Debug.Log("List<string> 类型序列化后的Json字符串"); ListSerializeJson(_List); Debug.Log("List<TestEntity> 类型序列化后的Json字符串"); ListSerializeJson(_ListTestEntities); Debug.Log("List<Dictionary<string, string>> 类型序列化后的Json字符串"); ListSerializeJson(_LisDic); //字典序列化后的字符串 Debug.Log("Dictionary<string, string> 类型序列化后的Json字符串"); DicSerializeJson(_Dic); Debug.Log("Dictionary<string, TestEntity> 类型序列化后的Json字符串"); DicSerializeJson(_DicEntities); Debug.Log("Dictionary<string, List<string>> 类型序列化后的Json字符串"); DicSerializeJson(_DicListStr); Debug.Log("Dictionary<string, List<TestEntity>> 类型序列化后的Json字符串"); DicSerializeJson(_DicListEntities); //实体序列化的字符串 Debug.Log("TestEntity类型序列化后的Json字符串"); EntitySerializeJson(_TestEntity); } } #endregion #region 公有方法 #endregion #region 私有方法 //初始化数据 private void InitData() { _List.Add("1011"); _List.Add("1012"); TestEntity testEntity1 = new TestEntity("1011", "3"); TestEntity testEntity2 = new TestEntity("1012", "3"); _DicEntities.Add("infos",testEntity1); _ListTestEntities.Add(testEntity1); _ListTestEntities.Add(testEntity2); _Dic.Add("任务号1", "1011"); _Dic.Add("任务号2", "1012"); _LisDic.Add(_Dic); _DicListStr.Add("infos",_List); _DicListEntities.Add("infos", _ListTestEntities); _TestEntity.TaskID = "1011"; _TestEntity.Priority = "3"; } //List序列化的Json字符串 private void ListSerializeJson<T>(List<T> data) { string str = JsonConvert.SerializeObject(data); Debug.Log(str); } //Dictionary序列化的Json字符串 private void DicSerializeJson<T>(Dictionary<string,T> data) { string str = JsonConvert.SerializeObject(data); Debug.Log(str); } //实体序列化的Json字符串 private void EntitySerializeJson<T>(T data) where T : class { string str = JsonConvert.SerializeObject(data); Debug.Log(str); } #endregion }//Class_end //测试实体 public class TestEntity { //任务ID public string TaskID { get; set; } //优先级 public string Priority { get; set; } public TestEntity() { } public TestEntity(string taskeID,string priority) { this.TaskID = taskeID; this.Priority = priority; } } }

 

 

 

最新回复(0)