先分享一個網站http://www.bejson.com/,這個是用來檢測Json文件的錯誤的,Json文件通常很差查找錯誤.node
看懂Json只須要四句話:json
對象表示爲鍵值對數組
數據由逗號分隔網絡
花括號保存對象app
方括號保存數組函數
這就是Json文件.不在過多介紹,重點不在這裏post
須要用到的API:網站
JsonMapper.ToJson();spa
JsonMapper.ToObject();excel
引用命名空間Using LitJson;
代碼:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using LitJson; 5 public class JsonTest : MonoBehaviour { 6 void Start() 7 { 8 //對象到Json 9 MyIphone myPhone = new MyIphone 10 { 11 appNum = 30, 12 phoneState = true, 13 appList = new List<string>() { "抖音", "BiliBili", "喜馬拉雅聽" } 14 }; 15 string jsonIpone = JsonMapper.ToJson(myPhone); 16 Debug.Log(jsonIpone); 17 18 19 //Json轉到對象 20 string myIphoneJson = "{'appNum':25,'phoneState':false,'appList':['WeChat','Sina','QQ']}"; 21 MyIphone myIPhone = JsonMapper.ToObject<MyIphone>(myIphoneJson); 22 Debug.Log("手機應用數量:"+myIPhone.appNum+"-手機開機狀態"+myIPhone.phoneState); 23 for (int i = 0; i < myIPhone.appList.Count; i++) 24 { 25 Debug.Log(myIPhone.appList[i]); 26 } 27 } 28 } 29 public class MyIphone 30 { 31 public int appNum; 32 public bool phoneState; 33 public List<string> appList; 34 }
代碼二:
1 using System.Collections; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Text; 5 using LitJson; 6 using UnityEngine; 7 public class DataNode// 該class用於json的時候不能有構造函數 8 { 9 public string CopyName; 10 public string CopyPosition; 11 public string CopyRotation; 12 } 13 public class DataCenter 14 { 15 public List<DataNode> List; 16 17 public DataCenter() 18 { 19 List =new List<DataNode>(); 20 } 21 } 22 public class JsonConvert : MonoBehaviour { 23 private string _txtPath; 24 private string _jsonPath; 25 void Start () 26 { 27 _jsonPath = Application.streamingAssetsPath + "/CopyInfo.json"; 28 _txtPath = Application.streamingAssetsPath + "/CopyInfo.txt"; 29 // Json的解析是很快的 網絡 30 ReadTextToJson(); 31 ReadJsonFromJsonPath(); 32 } 33 void ReadJsonFromJsonPath() 34 { 35 string jsondata = File.ReadAllText(_jsonPath); 36 List<DataNode> node = JsonMapper.ToObject<List<DataNode>>(jsondata); 37 Debug.LogError(node.Count); 38 } 39 void ReadTextToJson() 40 { 41 DataCenter dc = new DataCenter(); 42 using (StreamReader reader = new StreamReader(_txtPath,Encoding.UTF8)) 43 { 44 string tmpStr = string.Empty; 45 while ( !string.IsNullOrEmpty(tmpStr = reader.ReadLine())) 46 { 47 string[] infos = tmpStr.Split('_'); 48 DataNode _node = new DataNode(); 49 _node = infos(); 50 dc.List.Add(_node); 51 } 52 } 53 //數據讀取完畢 開始寫入json 傳遞的List<> 54 string jsonData = JsonMapper.ToJson(dc.List); 55 File.WriteAllText(_jsonPath,jsonData); 56 } 57 }
代碼三:
1 using System.Collections; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Text; 5 using UnityEngine; 6 using Excel; 7 using OfficeOpenXml; 8 using Excel.Core; 9 using System.Data; 10 using OfficeOpenXml.Style; 11 using UnityEditor; 12 using LitJson; 13 class CopyInfo //CopyInfo的構造 14 { 15 public string CopyName; 16 public Vector3 CopyPosition; 17 public Quaternion CopyRotation; 18 public CopyInfo(string name, Vector3 postion, Vector3 rotation) 19 { 20 CopyName = name; 21 CopyPosition = postion; 22 CopyRotation = Quaternion.Euler(rotation); 23 } 24 } 25 class JsonNode//JsonNode的構造 26 { 27 public string StrName; 28 public string StrPosition; 29 public string StrRotation; 30 public JsonNode(string name,string pos, string rot) 31 { 32 StrName = name; 33 StrPosition = pos; 34 StrRotation = rot; 35 } 36 } 37 class MyJsonData //MyJsonData的構造 38 { 39 public List<JsonNode> JsonDatalist; 40 41 public MyJsonData() 42 { 43 JsonDatalist = new List<JsonNode>(); 44 } 45 } 46 public class CreateManager : MonoBehaviour 47 { 48 private List<GameObject> _copyList; 49 private List<CopyInfo> _copyInfoList; 50 private MyJsonData _jsonData; 51 private string _excelPath; 52 private string _path; 53 //1. 讀文件 54 //2. 解析文件信息 55 //3. 實例化 56 //4. 實例化6個 57 //4.1 在3s中銷燬一個而且添加一個 58 #region 讀取存儲在unity的streamingAssets路徑中的json 59 void ReadJsonByJsonPath() 60 { 61 string path = Application.streamingAssetsPath + "/CopyInfo.json";//路徑 62 string readInfo =File.ReadAllText(path);//文件讀取過程 63 Debug.LogWarning(readInfo); 64 65 //類型是JsonNode 66 List<JsonNode> JsonDatalist = new List<JsonNode>();//由於沒有new對象!!! 67 JsonDatalist = JsonMapper.ToObject<List<JsonNode>>(readInfo);//重要代碼 68 Debug.LogError(JsonDatalist.Count); 69 } 70 #endregion 71 void Start() 72 { 73 _path = Application.streamingAssetsPath + "/CopyInfo.txt"; 74 _excelPath = Application.streamingAssetsPath + "/CopyInfo.xlsx"; 75 _copyInfoList = new List<CopyInfo>(); 76 _copyList = new List<GameObject>(); 77 _jsonData = new MyJsonData(); 78 WriteJsonByList(); 79 ReadJsonByJsonPath(); 80 } 81 #region 82 private void WriteJsonByList( ) 83 { 84 string jsonPath = Application.streamingAssetsPath 85 + "/CopyInfo.json"; 86 ////將一個list信息轉換爲json格式 87 ////定義一個class 這個class是什麼不重要 必須包含一個列表 88 _jsonData = new MyJsonData(); 89 using (StreamReader reader = new StreamReader(_path,Encoding.UTF8)) 90 { 91 string tmp = string.Empty; 92 while ( ! string.IsNullOrEmpty(tmp = reader.ReadLine())) 93 { 94 string[] infos = tmp.Split('_'); 95 _jsonData.JsonDatalist.Add(new JsonNode(infos[0], infos[1], infos[2])); 96 } 97 } 98 //jsondata 數據填充完成 99 string writeData = JsonMapper.ToJson(_jsonData);// 100 File.WriteAllText(jsonPath,writeData); 101 } 102 #endregion 103 private void WriteExcel(string path, List<CopyInfo> list) 104 { 105 FileInfo excelInfo = new FileInfo(path); 106 if (excelInfo.Exists) 107 { 108 excelInfo.Delete(); 109 excelInfo = new FileInfo(path); 110 } 111 //開始shiyong Excel 112 using (ExcelPackage package = new ExcelPackage(excelInfo)) 113 { 114 ExcelWorksheet sheet = package.Workbook.Worksheets.Add("TestInfo"); // 添加了一個工做表 115 sheet.Cells[1, 1].Value = "CopyName"; 116 sheet.Cells[1, 2].Value = "CopyPosition"; 117 sheet.Cells[1, 3].Value = "CopyRotation"; 118 for (int i = 0; i < _copyInfoList.Count; i++) 119 { 120 sheet.Cells[2 + i, 1 ].Value = _copyInfoList[i].CopyName; 121 sheet.Cells[2 + i, 2 ].Value = _copyInfoList[i].CopyPosition; 122 sheet.Cells[2 + i, 3 ].Value = _copyInfoList[i].CopyRotation; 123 } 124 sheet.Cells.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; 125 sheet.Cells.Style.VerticalAlignment = ExcelVerticalAlignment.Center; 126 sheet.Cells.Style.Font.Bold = true; 127 sheet.Cells.Style.Font.Name = "宋體"; 128 sheet.Cells.Style.Font.Size = 28; 129 sheet.Cells.AutoFitColumns(50, 150); 130 package.Save(); 131 } 132 AssetDatabase.Refresh(); 133 } 134 }