当前位置:网站首页>unity--newtonsoft.json解析
unity--newtonsoft.json解析
2022-06-27 09:26:00 【格拉格拉】
1.level1.txt json文件
{
"level": {
"floorCount": 2,
"maxNum": 2,
"left": 1,
"right": 1,
"time":[
{
"floor1":0.2
},
{
"floor2":0.5
}
]
}
}
2.LevelEntity.cs json實體類
[Serializable]
public class LevelEntity
{
public int _floorCount;
public int _maxNum;
public int _left;
public int _right;
public List<float> _time = new List<float>();
}
3.讀取StreamingAssets文件夾下的json文件
public IEnumerator LoadLevel(string fileName)
{
string filePath = "";
string result = "";
#if UNITY_EDITOR
filePath = Application.dataPath + "/StreamingAssets/" + fileName + ".txt";
#elif UNITY_IPHONE
filePath = Application.dataPath +"/Raw/"+fileName+".txt";
#elif UNITY_ANDROID
filePath = “jar:file://” + Application.dataPath + “!/assets//"+fileName+".txt";
#endif
Debug.LogError("FilePath:" + filePath);
if (filePath.Contains("://"))
{
UnityWebRequest www = UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
if (www.isDone == false)
{
Debug.LogError("read json error:" + fileName);
}
result = www.downloadHandler.text;
}
else
{
result = System.IO.File.ReadAllText(filePath);
}
Debug.Log(result);
JObject jsonData = JObject.Parse(result);
JToken jToken = jsonData["Data"];
le = new LevelEntity
{
_floorCount = int.Parse(jToken["floorCount"].ToString()),
_maxNum = int.Parse(jToken["maxNum"].ToString()),
_random = int.Parse(jToken["random"].ToString()),
_left = int.Parse(jToken["left"].ToString()),
_right = int.Parse(jToken["right"].ToString())
};
}
4.讀取Resources文件夾下的json文件
//讀取Resources下的json文件
public void ReadJson(string fileName)
{
//獲得Json字符串
string json = "";
TextAsset text = Resources.Load<TextAsset>("levels/" + fileName);
json = text.text;
if (string.IsNullOrEmpty(json))
{
json = "";
Debug.LogError(fileName + "...json is null or empty!");
}
Debug.Log(json);
JObject jsonData = JObject.Parse(json);
JToken jToken = jsonData["level"];
JArray ja = JArray.Parse(jsonData["level"]["time"].ToString());
le = new LevelEntity();
le._floorCount = int.Parse(jToken["floorCount"].ToString());
le._maxNum = int.Parse(jToken["maxNum"].ToString());
le._left = int.Parse(jToken["left"].ToString());
le._right = int.Parse(jToken["right"].ToString());
for (int i = 0; i < ja.Count; i++)
{
string str1 = ja[i].ToString().Replace("{", "").Replace("}", "");
string[] str2 = str1.Split(':');
le._time.Add(float.Parse(str2[1]));
}
}
边栏推荐
- 浏览器的markdown插件显示不了图片
- CLassLoader
- Apache POI的读写
- 经典的一道面试题,涵盖4个热点知识
- Today's three interviews demo[integer ASCII class relationship]
- Principle and application of the most complete H-bridge motor drive module L298N
- [vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy
- Rockermq message sending and consumption mode
- 今日3大面试Demo[Integer ASCII 类关系]
- MATLAB小技巧(19)矩阵分析--主成分分析
猜你喜欢
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
Advanced mathematics Chapter 7 differential equations
Source insight 工具安装及使用方法
Principle and application of the most complete H-bridge motor drive module L298N
RMAN-08137 主库无法删除归档文件
Five page Jump methods for wechat applet learning
How do I get the STW (pause) time of a GC (garbage collector)?
SVN版本控制器的安装及使用方法
力扣84柱状图中最大的矩形
MATLAB小技巧(19)矩阵分析--主成分分析
随机推荐
VIM from dislike to dependence (19) -- substitution
Object含有Copy方法?
ucore lab4
Flow chart of Alipay wechat payment business
HiTek电源维修X光机高压发生器维修XR150-603-02
多个类的设计
[cloud native] 2.3 kubernetes core practice (Part 1)
Video file too large? Use ffmpeg to compress it losslessly
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
(original) custom drawable
this,构造器,静态,之间调用,必须搞懂啊!
ucore lab5
IMX8QXP DMA资源和使用(未完结)
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
隐私计算FATE-离线预测
DV scroll board width of datav rotation table component
支付宝微信支付业务流程图
Fake constructor???
Modify the contents of /etc/crontab file directly, and the scheduled task will not take effect
今日3大面试Demo[Integer ASCII 类关系]