Unity 读取Json常用的两种方式
使用的是Litjson
1、读取本地Json
public void ReadJson() { StreamReader streamReader = new StreamReader(Application.dataPath + "/XXX.json"); string str = streamReader.ReadToEnd(); json = JsonMapper.ToObject(str); }2、请求http
private IEnumerator GetInterface(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
{
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
Debug.LogError(uri + "请求错误:" + webRequest.error);
}
else
{
Debug.Log(webRequest.downloadHandler.text);
//保存本地
string savePath = Application.streamingAssetsPath + "/" + "XXX.json";
File.WriteAllText(savePath, Regex.Unescape(webRequest.downloadHandler.text));
//读取
StreamReader streamReader = new StreamReader(savePath);
string str = streamReader.ReadToEnd();
JsonData json = JsonMapper.ToObject(str);
streamReader.Close();
}
}
}
- 上一篇:常见的手别识别硬件方案 2021/3/5
- 下一篇:UE4调用VR外设WISEGLOVE数据手套的LIB和DLL 2021/3/4