Unity中Json转换

using UnityEngine;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json;
public class  LoadJson 
{
    public static string defaultPath = null;

    void Start()
    {
        defaultPath = Application.dataPath + "/document.json";

    }

    /// <summary>
    /// 加载本地文件
    /// </summary>
    public static T LoadJsonFromFile<T>(string path)
    {
        
        if (!File.Exists(path))
        {
            Debug.Log("nothing");
            return default(T);
        }

        StreamReader sr = new StreamReader(path);

        if (sr == null)
        {
            return default(T);
        }
        string json = sr.ReadToEnd();

        if (json.Length > 0)
        {
            //Debug.Log(json.Length);
            //Debug.Log(json);
            
            
            return JsonConvert.DeserializeObject<T>(json);
        }

        return default(T);
    }


    /// <summary>
    /// 加载网络数据
    /// </summary>
    public static T LoadJsonFromNet<T>(string result)
    {
        Debug.Log(result.Length);
        return JsonUtility.FromJson<T>(result);
    }
}
需要用到一个Newtonsoft.Json.dll,很好用,比unity自带的JsonUtility好用很多

猜你喜欢

转载自blog.csdn.net/skylovecrayon/article/details/70833464
今日推荐