Unity解析JSON的几种方式

Unity解析JSON的几种方式

1.使用JsonUtility(Unity自带)解析数据

踩坑

*用于接收的JSON实体类需要声明[Serializable] 序列化。
*使用Unity自带方法时,实体类如果是属性成员(public string mname{get;set;})的话,
*在序列化的时候会缺失这些成员,导致解析不出来。将属性改为字段即(public string mname;)。

var uri = new System.Uri(Path.Combine(Application.streamingAssetsPath, "demoText.json"));
        UnityWebRequest request = UnityWebRequest.Get(uri);
        yield return request.SendWebRequest();
        if (request.isNetworkError)
        {
            Debug.Log(request.error);
        }
        else
        {
            string jsonStr = request.downloadHandler.text;
            RootDate Data= JsonUtility.FromJson<RootDate >(jsonStr);
 
        }
      

2.使用Newtonsoft.Json dll解析json 链接: link

var uri = new System.Uri(Path.Combine(Application.streamingAssetsPath, "demoText.json"));
        UnityWebRequest request = UnityWebRequest.Get(uri);
        yield return request.SendWebRequest();
        if (request.isNetworkError)
        {
            Debug.Log(request.error);
        }
        else
        {
            string jsonStr = request.downloadHandler.text;
            RootDate Data = JsonConvert.DeserializeObject<RootDate >(jsonStr);
 
        }
 

3.使用LitJson解析数据

var uri = new System.Uri(Path.Combine(Application.streamingAssetsPath, "demoText.json"));
        UnityWebRequest request = UnityWebRequest.Get(uri);
        yield return request.SendWebRequest();
        if (request.isNetworkError)
        {
            Debug.Log(request.error);
        }
        else
        {
            string jsonStr = request.downloadHandler.text;
            RootDate Data= JsonMapper.ToObject<RootDate>(jsonStr);
 
        }

你可能感兴趣的:(unity,json,游戏引擎)