1.第一種unity自帶解析的API JsonUtility 讀取Json 不需要dll文件
2.第二種 Newtonsoft.Json dll解析json 讀取json 需要dll文件(下載地址) 免費的
json格式如下
{ "name": [ { "age": 28, "sex": "不男不女" }, { "age": 7, "sex": "東方不敗" }, { "age": 20, "sex": "男" }, { "age": 15, "sex": "女" } ] }
Json文件我是保存在StreamingAssets 下的 textJson.json
c#腳本如下
using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using System; using Newtonsoft.Json; [Serializable] //序列化 public class AA { public BB[] name; } [Serializable] //序列化 public class BB { public int age; public string sex; } public class Parsing_json : MonoBehaviour { public string jsonpath = Application.streamingAssetsPath + "/textJson.json"; // Start is called before the first frame update void Start() { #region 第一種unity自帶解析的API JsonUtility 讀取Json //string json = File.ReadAllText(Application.streamingAssetsPath + "/textJson.json"); //AA aa = JsonUtility.FromJson<AA>(json); //Debug.Log("年齡" + aa.name[0].age + "性別" + aa.name[0].sex); //Debug.Log("年齡" + aa.name[1].age + "性別" + aa.name[1].sex); //Debug.Log("年齡" + aa.name[2].age + "性別" + aa.name[2].sex); //Debug.Log("年齡" + aa.name[3].age + "性別" + aa.name[3].sex); #endregion #region 第二種 Newtonsoft.Json dll解析json 讀取json //string json = File.ReadAllText(Application.streamingAssetsPath + "/textJson.json"); //AA obj = JsonConvert.DeserializeObject<AA>(json); //Debug.Log("年齡: " + obj.name[0].age + "性別: " + obj.name[0].sex); //Debug.Log("年齡: " + obj.name[1].age + "性別: " + obj.name[1].sex); #endregion #region 創建json 保存到json文件 //// 創建Json //BB p1 = new BB(); //p1.sex = "不男不女"; //p1.age = 28; //string json = JsonUtility.ToJson(p1); //BB p2 = new BB(); //p2.sex = "東方不敗"; //p2.age = 7; //BB[] ps = new BB[] { p1, p2 }; //AA persons = new AA(); //persons.name = ps; //json = JsonUtility.ToJson(persons); //if (!System.IO.File.Exists (jsonpath)) //{ // File.CreateText(jsonpath).Dispose(); //} //File.WriteAllText(jsonpath, json, System.Text.Encoding.UTF8); #endregion //解析Json string jso = File.ReadAllText(jsonpath); AA obj = JsonUtility.FromJson<AA>(jso); foreach (var item in obj.name) { Debug.Log("年齡: " + item.age + "性別: " + item.sex); } } }
打印出來如下
2.完整腳本掛上運行測試即可

using Newtonsoft.Json.Linq; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using System; using Newtonsoft.Json; [Serializable] //序列化 public class Lint { public List<Setlist>data = new List<Setlist>(); } [Serializable] //序列化 public class Setlist { public string name; //名稱 public string profession; //專業 public string spec; //規格 public string number; //資產編號 public string brand; //品牌 public string time; //維保時間 public string recorder; //記錄人 } public class Parsing_json : MonoBehaviour { public string jsonpath = Application.streamingAssetsPath + "/textJson.json"; Dictionary<int, Setlist> DicList = new Dictionary<int, Setlist>(); Dictionary<int, List<string>> strlisting = new Dictionary<int, List<string>>(); Lint lint = new Lint(); /// <summary> /// 清空列表 /// </summary> public void Clear_List() { lint.data.Clear(); } //public void SetDictionary(string profession,string name,string number,string brand,string time,string recorder) public void SetDictionary(List<string> _L) { Setlist setlist = new Setlist(); setlist.name = _L[0]; setlist.profession = _L[1]; setlist.spec = _L[2]; setlist.number = _L[3]; setlist.brand = _L[4]; setlist.time = _L[5]; setlist.recorder = _L[6]; if (!System.IO.File.Exists(jsonpath)) { File.CreateText(jsonpath).Dispose(); } lint.data.Add(setlist); //string json = JsonUtility.ToJson(lint); string json = JsonConvert.SerializeObject(lint); File.WriteAllText(jsonpath, json, System.Text.Encoding.UTF8); } public void SetDictionary(Dictionary<int, List<string>> strlist) { Clear_List(); for (int i = 0; i < strlist.Count; i++) { SetDictionary(strlist[i]); } } // Start is called before the first frame update void Start() { List<string> sds = new List<string>(); sds.Add("0"); sds.Add("1"); sds.Add("2"); sds.Add("3"); sds.Add("4"); sds.Add("5"); sds.Add("9"); strlisting.Add(0, sds); List<string> sdss = new List<string>(); sdss.Add("6"); sdss.Add("7"); sdss.Add("8"); sdss.Add("9"); sdss.Add("10"); sdss.Add("11"); sdss.Add("19"); strlisting.Add(1, sdss); List<string> sdssd = new List<string>(); sdssd.Add("12"); sdssd.Add("13"); sdssd.Add("14"); sdssd.Add("15"); sdssd.Add("16"); sdssd.Add("17"); sdssd.Add("18"); strlisting.Add(2, sdssd); SetDictionary(strlisting); Parse_Json(jsonpath); } ////解析Json public void Parse_Json(string path) { string jso = File.ReadAllText(path); //Lint obj = JsonUtility.FromJson<Lint>(jso); unity 解析 Lint obj = JsonConvert.DeserializeObject <Lint>(jso); for (int i = 0; i < obj.data.Count; i++) { Setlist setlist = new Setlist(); setlist.name = obj.data[i].name; setlist.profession = obj.data[i].profession; setlist.spec = obj.data[i].spec; setlist.number = obj.data[i].number; setlist.brand = obj.data[i].brand; setlist.time = obj.data[i].time; setlist.recorder = obj.data[i].recorder; DicList.Add(i, setlist); } Debug.Log(DicList.Count); } }
❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
最后警惕一下
如果打印出來的中文出現亂碼
解決辦法 在文件夾中用記事本打開json文件,另存為一下(改一下編碼格式為UTF8即可)名字不用改它會自動覆蓋原來的json文件
如果喜歡請點個贊吧 ! 感謝
❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤