C# 结构体和List类型数据转Json数据保存和读取

时间:2021-05-08 22:34:32

C#  结构体和List<T>类型数据转Json数据保存和读取

 一.结构体转Json

     public struct FaceLibrary
{
public string face_name;
public byte[] face_Feature;
} //序列化结构体
facelibrary = new FaceLibrary();
facelibrary.face_name = "zhangsan";
facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff }; MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(FaceLibrary));
ser.WriteObject(stream1, facelibrary);
stream1.Position = ;
StreamReader sr = new StreamReader(stream1);
Console.Write("JSON form of Person object: ");
Console.WriteLine(sr.ReadToEnd()); //打印结果:
JSON form of Person object: [{"face_Feature":[,,,,],"face_name":"zhangsan"} //反序列化结构体
stream1.Position = ;
FaceLibrary p2 = (FaceLibrary)ser.ReadObject(stream1);
Console.WriteLine(p2.face_name);
Console.WriteLine(p2.face_Feature);
//输出结果:
wangjin01
System.Byte[] 说明: 通过如上操作,能够将结构体的数据转为Json数据 方法二: 将List<T> 结构的数据转为Json数据 public struct FaceLibrary
{
public string face_name;
public byte[] face_Feature;
} List<FaceLibrary> listfacex = new List<FaceLibrary>(); //序列化List<T>
facelibrary = new FaceLibrary();
facelibrary.face_name = "zhangsan01";
facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan02";
facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan03";
facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan04";
facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan05";
facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
listfacex.Add(facelibrary); MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<FaceLibrary>));
ser.WriteObject(stream1, listfacex);
stream1.Position = ;
StreamReader sr = new StreamReader(stream1);
Console.Write("JSON form of Person object: ");
Console.WriteLine(sr.ReadToEnd()); //输出结果:
JSON form of Person object: [
{"face_Feature":[,,,,],"face_name":"zhangsan01"},
{"face_Feature":[,,,,],"face_name":"zhangsan02"},
{"face_Feature":[,,,,],"face_name":"zhangsan03"},
{"face_Feature":[,,,,],"face_name":"zhangsan04"},
{"face_Feature":[,,,,],"face_name":"zhangsan05"}
] //反序列化List<T>
stream1.Position = ;
List<FaceLibrary> p2 = (List<FaceLibrary>)ser.ReadObject(stream1);
Console.WriteLine(p2[].face_name);
Console.WriteLine(p2[].face_Feature);
//输出结果:
zhangsan01
System.Byte[] 三.测试应用
/*
应用需求:
1. 通过List<T>将结构体类型<T>的多个数据加载到List<T>中;
2. 将List<T>数据转为Json数据,并写入到文件中;
3. 从文件中读取保存的Json数据,并还原成List<T>数据供后续调用; //说明: JavaScriptSerializer 需要导入相应的库,
命名空间: System.Web.Script.Serialization
程序集: System.Web.Extensions(位于 System.Web.Extensions.dll)
参考文件:https://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer.aspx
*/ //1.定义结构体和List<T>
public struct FaceLibrary
{
public string face_name;
public byte[] face_Feature;
}
FaceLibrary facelibrary;
List<FaceLibrary> listfacex = new List<FaceLibrary>(); //2. 添加数据到List<T>中
facelibrary.face_name = "zhangsan01";
facelibrary.face_Feature = new byte[] { 0xaa, 0x01, 0x11, 0x21, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan02";
facelibrary.face_Feature = new byte[] { 0xaa, 0x02, 0x12, 0x22, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan03";
facelibrary.face_Feature = new byte[] { 0xaa, 0x03, 0x13, 0x23, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan04";
facelibrary.face_Feature = new byte[] { 0xaa, 0x04, 0x14, 0x24, 0xff };
listfacex.Add(facelibrary); facelibrary.face_name = "zhangsan05";
facelibrary.face_Feature = new byte[] { 0xaa, 0x05, 0x15, 0x25, 0xff };
listfacex.Add(facelibrary); //3. 将List<T>数据转为Json数据
String str = JsonListToString(listfacex);
//JSON序列化,将List<T>转换为String
private String JsonListToString (List<FaceLibrary> list)
{
JavaScriptSerializer Serializerx = new JavaScriptSerializer();
string changestr = Serializerx.Serialize(list);
return changestr;
} //4. 将Json数据写入文件系统
FileWrite("test.txt",str)
//写入文件
private void FileWrite(string filepath,string writestr)
{
FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.Write(writestr);
sw.Close();
fs.Close();
} //5. 从文件中读取Json数据 string str = FileRead("test.txt");
//读取文件
private string FileRead(string filepath)
{
FileStream fs = new FileStream(filepath, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadToEnd();
sr.Close();
fs.Close();
return str;
} //6. 将Json数据转换为List<T>数据 List<FaceLibrary> listface = StringToJsonList(str);
//JSON反序列化,将List<T>转换为String
private List<FaceLibrary> StringToJsonList(string str)
{
JavaScriptSerializer Serializer = new JavaScriptSerializer();
List<FaceLibrary> face = Serializer.Deserialize<List<FaceLibrary>>(str);
return face;
} //以上方法基本能够解决上述问题; 以上代码基于参考如下代码,
/*
// C#将Json字符串反序列化成List对象类集合 using System.IO;
using System.Web.Script.Serialization;
using System.Runtime.Serialization.Json;
public static List<T> JSONStringToList<T>(this string JsonStr)
{ JavaScriptSerializer Serializer = new JavaScriptSerializer(); List<T> objs = Serializer.Deserialize<List<T>>(JsonStr); return objs; } public static T Deserialize<T>(string json)
{ T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{ DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms);
} } //好了,我们来测试下
string JsonStr = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";
List<Product> products = new List<Product>();
products = JSONStringToList<Product>(JsonStr);
//Response.Write(products.Count());
foreach (var item in products)
{
Response.Write(item.Name + ":" + item.Price + "<br />");
}
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
}
结果:
苹果:5.5
橘子:2.5
柿子:16 */