当前位置:   article > 正文

unity--json读取与解析_unity读取并解析josn配置文件

unity读取并解析josn配置文件

目录

1.level1.txt json文件

2.LevelEntity.cs json实体类

3.读取StreamingAssets文件夹下的json文件

4.读取Resources文件夹下的json文件

5.newtonsoft.Dll下载

6.实体类转json 存本地



1.level1.txt json文件

  1. {
  2. "level": {
  3. "floorCount": 2,
  4. "maxNum": 2,
  5. "left": 1,
  6. "right": 1,
  7. "time":[
  8. {
  9. "floor1":0.2
  10. },
  11. {
  12. "floor2":0.5
  13. }
  14. ]
  15. }
  16. }

2.LevelEntity.cs json实体类

  1. [Serializable]
  2. public class LevelEntity
  3. {
  4. public int _floorCount;
  5. public int _maxNum;
  6. public int _left;
  7. public int _right;
  8. public List<float> _time = new List<float>();
  9. }

3.读取StreamingAssets文件夹下的json文件

  1. public IEnumerator LoadLevel(string fileName)
  2.     {
  3.         string filePath = "";
  4.         string result = "";
  5. #if UNITY_EDITOR
  6.         filePath = Application.dataPath + "/StreamingAssets/" + fileName + ".txt";
  7. #elif UNITY_IPHONE
  8.         filePath = Application.dataPath +"/Raw/"+fileName+".txt";
  9. #elif UNITY_ANDROID
  10.         //filePath = “jar:file://” + Application.dataPath + “!/assets//"+fileName+".txt";
  11. filePath = Application.streamingAssetsPath+fileName+".txt";
  12. #endif
  13.         Debug.LogError("FilePath:" + filePath);
  14.         if (filePath.Contains("://"))
  15.         {
  16.             UnityWebRequest www = UnityWebRequest.Get(filePath);
  17.             yield return www.SendWebRequest();
  18.             if (www.isDone == false)
  19.             {
  20.                 Debug.LogError("read json error:" + fileName);
  21.             }
  22.             result = www.downloadHandler.text;
  23.         }
  24.         else
  25.         {
  26.             result = System.IO.File.ReadAllText(filePath);
  27.         }
  28.         Debug.Log(result);
  29.         JObject jsonData = JObject.Parse(result);
  30.         JToken jToken = jsonData["Data"];
  31.         le = new LevelEntity
  32.         {
  33.             _floorCount = int.Parse(jToken["floorCount"].ToString()),
  34.             _maxNum = int.Parse(jToken["maxNum"].ToString()),
  35.             _random = int.Parse(jToken["random"].ToString()),
  36.             _left = int.Parse(jToken["left"].ToString()),
  37.             _right = int.Parse(jToken["right"].ToString())
  38.         };
  39.     }

上面报错的话,读取方式二:

  1. public IEnumerator LoadConfig(string fileName)
  2. {
  3. string filePath = Path.Combine(Application.streamingAssetsPath,fileName);
  4. string url = "";
  5. if (Application.platform == RuntimePlatform.Android)
  6. {
  7. // Android平台的URL前缀
  8. url = "jar:file://" + filePath;
  9. }
  10. else if (Application.platform == RuntimePlatform.IPhonePlayer)
  11. {
  12. // iOS平台的URL前缀
  13. url = "file://" + filePath;
  14. }
  15. else
  16. {
  17. // 其他平台的URL前缀
  18. url = "file://" + filePath;
  19. }
  20. using (UnityWebRequest www = UnityWebRequest.Get(url))
  21. {
  22. yield return www.SendWebRequest();
  23. if (www.result == UnityWebRequest.Result.Success)
  24. {
  25. // 读取文件成功
  26. string fileContents = www.downloadHandler.text;
  27. Debug.Log("文件内容: " + fileContents);
  28. //ToolClass.DebugInfo("字符串内容是:" + www.downloadHandler.text);
  29. switch (fileName)
  30. {
  31. case EnumTableName.ZiMuZu:
  32. comJson_ZiMuZu = JsonUtility.FromJson<Json_ZiMuZu>(www.downloadHandler.text);
  33. cntLoadedConfig++;
  34. break;
  35. }
  36. }
  37. else
  38. {
  39. // 读取文件失败
  40. Debug.LogError("读取文件失败: " + www.error);
  41. }
  42. }
  43. }

4.读取Resources文件夹下的json文件

  1. //读取Resources下的json文件
  2. public void ReadJson(string fileName)
  3. {
  4. //获得Json字符串
  5. string json = "";
  6. TextAsset text = Resources.Load<TextAsset>("levels/" + fileName);
  7. json = text.text;
  8. if (string.IsNullOrEmpty(json))
  9. {
  10. json = "";
  11. Debug.LogError(fileName + "...json is null or empty!");
  12. }
  13. Debug.Log(json);
  14. JObject jsonData = JObject.Parse(json);
  15. JToken jToken = jsonData["level"];
  16. JArray ja = JArray.Parse(jsonData["level"]["time"].ToString());
  17. le = new LevelEntity();
  18. le._floorCount = int.Parse(jToken["floorCount"].ToString());
  19. le._maxNum = int.Parse(jToken["maxNum"].ToString());
  20. le._left = int.Parse(jToken["left"].ToString());
  21. le._right = int.Parse(jToken["right"].ToString());
  22. for (int i = 0; i < ja.Count; i++)
  23. {
  24. string str1 = ja[i].ToString().Replace("{", "").Replace("}", "");
  25. string[] str2 = str1.Split(':');
  26. le._time.Add(float.Parse(str2[1]));
  27. }
  28. }

5.newtonsoft.Dll下载

去下载
 

6.实体类转json 存本地

  1. //存本地--单个
  2. string str = JsonUtility.ToJson(entity, true);
  3. string filePath = Application.streamingAssetsPath + "/sbHaveList.json";
  4. using (StreamWriter sw = new StreamWriter(filePath))
  5. {
  6. sw.WriteLine(str);
  7. sw.Close();
  8. sw.Dispose();
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/123484
推荐阅读
相关标签
  

闽ICP备14008679号