因为需要观察对象的数据,所以寻找能自动描述对象中字段名和其值的描述类,咨询了不少人,都推荐使用json,但是json使用起来有一点额外的操作.
需要在文件中引用
using System.Web.Script.Serialization;
可是添加了发现提示报错,找不到空间名字,原来还需要在项目引用中添加该引用:
我使用的是VS2010,在.NET引用中找不到它,查阅资料,还要到C盘中去找到并添加.我找到的地址如下:
从添加引用,浏览中添加后,就可以正常使用了.
public class test //自定义的数据类 { //public Dictionary<int, int> dary = new Dictionary<int, int>(); public List<List<int>> sary = new List<List<int>>(); public List<int> nary = new List<int>(); public xxx mx = new xxx(); public wori wr = new wori(); public Tuple<int, int, int, string> mt = new Tuple<int, int, int, string>(1, 1, 1, "aa"); public test() { int[] nnary = new int[5] { 1, 2, 3, 45, 7 }; nary = new List<int>(nnary); sary = new List<List<int>>(); sary.Add(ZCL.Tbas.MEarray.CreatIntRand(10, 1, 1000)); sary.Add(ZCL.Tbas.MEarray.CreatIntRand(10, 1, 1000)); sary.Add(ZCL.Tbas.MEarray.CreatIntRand(10, 1, 1000)); sary.Add(ZCL.Tbas.MEarray.CreatIntRand(10, 1, 1000)); sary.Add(ZCL.Tbas.MEarray.CreatIntRand(10, 1, 1000)); sary.Add(ZCL.Tbas.MEarray.CreatIntRand(10, 1, 1000)); } }
test md2 = new test(); //通过以下代码实现 JavaScriptSerializer serializer = new JavaScriptSerializer(); StringBuilder sb = new StringBuilder(); serializer.Serialize(md2, sb); ME.MsgShow(sb.ToString());
最后显示结果为:
{"sary":[[950,652,375,867,923,494,271,923,897,522],[624,631,8,505,671,508,888,827,19,983],[478,623,576,323,457,735,776,670,528,497],[237,813,868,350,901,512,303,135,374,808],[656,670,553,478,83,837,759,501,409,409],[348,473,931,859,751,328,76,53,411,189]],"nary":[1,2,3,45,7],"mx":{"ma":20,"mb":100,"sa":"abc","sb":"33333","sc":"ewr"},"wr":{"md":0,"ma":0,"sa":null,"sb":null,"sc":null},"mt":{"Item1":1,"Item2":1,"Item3":1,"Item4":"aa"}}
注意: JSON是无法直接序列化Dictionary,需要将其转化为List.
时间: 2024-11-05 02:43:10