StringBuilder sb = new StringBuilder();
JsonWriter writer = new JsonWriter(sb);
writer.WriteArrayStart();
writer.WriteObjectStart();
writer.WritePropertyName("ObjName");
writer.Write(key);
writer.WritePropertyName("ClassName");
writer.Write(str_className);
writer.WriteObjectEnd();
writer.WriteArrayEnd();
这是已经生成了 sb.tostring();
解析 生成的json
string json = sb.tostring(); JsonData data = new JsonData(); data = JsonMapper.ToObject(json); foreach (JsonData item in data) { string objname = (string)item["ObjName"]; // GameObject obj = GameObject.Find(objname); string classname = (string)item["ClassName"]; string[] _classname = classname.Split(‘|‘); List<string> list_className = new List<string>(); for (int i = 0; i < _classname.Length; i++) { list_className.Add(_classname[i]); } dic.Add(objname, list_className); }
将json 存储到本地
private void Save(string jsong) { //string values = JsonMapper.ToJson(dic); string values = jsong; Debug.Log(values); //找到当前路径 FileInfo file = new FileInfo(filePath+ "/Dependence.bytes"); //判断有没有文件,有则打开文件,,没有创建后打开文件 StreamWriter sw = file.CreateText(); //ToJson接口将你的列表类传进去,,并自动转换为string类型 // string json = JsonMapper.ToJson(personList.dictionary); //将转换好的字符串存进文件, sw.WriteLine(values); //注意释放资源 sw.Close(); sw.Dispose(); }
Unity 生成/解析 Json, Json数组 存储到本地
原文:https://www.cnblogs.com/DGJS/p/13693078.html