Json的序列化和反序列化
1.定義數(shù)據(jù)類
[Serializable]
public class ZoomPoint
{
// 點(diǎn)名稱, 將作為Key被字典存儲(chǔ)
public string name;
// 軸心X坐標(biāo)
public Vector2 pivot = Vector2.one / 2;
// 放大倍率,小于1是為縮小倍率,小于0是取絕對(duì)值,不允許原點(diǎn)對(duì)稱縮放,需要保證計(jì)算軸心邏輯正確
// 默認(rèn)設(shè)為1.5f 放大倍率
public float zoomMagnification = 5f;
// 改變的需要的時(shí)間,默認(rèn)設(shè)為1f
public float time = 1.0f;
public override string ToString()
{
return $"name = {this.name}, pivot = ({pivot.ToString()}), zoomMagnification = {this.zoomMagnification}, time = {this.time}";
}
}
2. 定義Json存儲(chǔ)類
// 數(shù)據(jù)存儲(chǔ)接受類
[Serializable]
public class Data{
// public ZoomPoint[] zoomPoints;
// public Dictionary<string, ZoomPoint> zoomPoints;
public List<ZoomPoint> zoomPoints;
}
3.序列化
// 寫(xiě)入數(shù)據(jù)
public void WriteDataTest(){
Data data = new()
{
zoomPoints = new List<ZoomPoint>()
};
ZoomPoint point1 = new ZoomPoint
{
name = "1",
pivot = new Vector2(0.75f,0.75f)
};
ZoomPoint point2 = new ZoomPoint
{
name = "2",
pivot = new Vector2(0.5f,0.5f)
};
data.zoomPoints[0] = point1;
data.zoomPoints[1] = point2;
string js = JsonUtility.ToJson(data);
// 獲取項(xiàng)目路徑
string fileUrl;
if(filePath == ""){
fileUrl = Application.streamingAssetsPath + jsonFileName;
}else{
fileUrl = filePath;
}
using(StreamWriter sw = new StreamWriter(fileUrl))
{
sw.WriteLine(js); //保存數(shù)據(jù)
sw.Close();
sw.Dispose();
}
}
4.反序列化
public Data ReadData(){
// 獲取文件路徑
string fileUrl;
if(filePath == ""){
fileUrl = Application.streamingAssetsPath + jsonFileName;
}else{
fileUrl = filePath;
}
//讀取文件
string readDate;
using (StreamReader sr = File.OpenText(fileUrl)){
readDate = sr.ReadLine();
sr.Close();
}
Data data = JsonUtility.FromJson<Data>(readDate);
// 分配內(nèi)存
if(data == null ){
data = new Data() {
zoomPoints = new List<ZoomPoint>()
};
return data;
}
// 數(shù)據(jù)保存到字典里
foreach(ZoomPoint zp in data.zoomPoints){
dict.TryAdd(zp.name, zp);
}
return data;
}
數(shù)據(jù)存儲(chǔ)效果:
Ps: Unity C# Json 序列化換行
在ToJson中使用兩個(gè)參數(shù),第二個(gè)參數(shù)PrettyPrint 輸入True
private void JsonText() {
Node node = new Node() {
name = "Node_01",
pos = new(0, 0)
};
string filePath = Application.streamingAssetsPath + "/Json_01.json";
string str = JsonUtility.ToJson(node, true);
using (StreamWriter sw = new StreamWriter(filePath)) {
sw.WriteLine(str); // 保存數(shù)據(jù)
sw.Close(); // 關(guān)閉文檔
sw.Dispose();
}
Debug.Log(str);
Grid grid = new() {
nodes = new List<Node>()
};
Node node1 = new Node() {
name = "Node_01",
pos = new Vector2(0, 0)
};
Node node2 = new Node() {
name = "Node_02",
pos = new Vector2(0,1)
};
grid.nodes.Add(node1);
grid.nodes.Add(node2);
string filePath_01 = Application.streamingAssetsPath + "/Json_02.json";
string str_01 = JsonUtility.ToJson(grid, true);
using (StreamWriter sw = new StreamWriter(filePath_01)) {
sw.WriteLine(str_01); // 保存數(shù)據(jù)
sw.Close(); // 關(guān)閉文檔
sw.Dispose();
}
Debug.Log("數(shù)據(jù)序列化完成");
}
[Serializable]
public class Node {
public string name;
public Vector2 pos;
}
[Serializable]
public class Grid {
public List<Node> nodes;
}
效果:
Xml的序列化和反序列化
1.定義數(shù)據(jù)類
public class XmlText {
public string name;
public string value;
public List<int> list;
public override string ToString() {
return $"name = {name}, value = {value}, list = {list}";
}
}
2.序列化
引用:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-639921.html
using System.Io;
using System.Xml.Serialization;
public void Init() {
test = new XmlText() {
name = "Xml測(cè)試",
value = "value",
list = new List<int>()
};
test.list.Add(1);
test.list.Add(3);
test.list.Add(100);
}
// 序列化Xml
public void XmlSerialize() {
// 定義流文件
FileStream fileStream = new FileStream(Application.streamingAssetsPath + "/text.xml",FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
// 定義寫(xiě)入流
StreamWriter sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
// 位于System.Xml.Serialization 中的Xml序列化
XmlSerializer xml = new XmlSerializer(test.GetType());
// 將類序列化寫(xiě)入流中
xml.Serialize(sw, test);
// 關(guān)閉流
sw.Close();
fileStream.Close();
}
3.反序列化
public XmlText Deserialize() {
// 流文件
FileStream fs = new FileStream(Application.streamingAssetsPath + "/text.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
// 定義序列化類
XmlSerializer xml = new XmlSerializer(typeof(XmlText));
// 反向序列化
XmlText result = (XmlText)xml.Deserialize(fs);
fs.Close();
return result;
}
結(jié)果:
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-639921.html
到了這里,關(guān)于Untiy Json和Xml的序列化和反序列化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!