在Unity中json文件的解析方式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;

public class DataManager<T>
{
    private string filePath;

    private static DataManager<T> instance;

    private DataManager()
    {
        filePath = Application.streamingAssetsPath + "/data.json";
    }

    public static DataManager<T> GetInstance()
    {
        if (null == instance)
        {
            instance = new DataManager<T>();
        }
        return instance;
    }


    public List<T> LoadAll()
    {
        string str = File.ReadAllText(filePath);
        T[] rows = JsonMapper.ToObject<T[]>(str);
        if(rows.Length>0)
        {
            return new List<T>(rows);
        } 
        return null;
    }

    public void Save(List<T> data)
    {
        string str = JsonMapper.ToJson(data);
        File.WriteAllText(filePath, str);
    }
}

  

猜你喜欢

转载自www.cnblogs.com/clhxxlcj/p/10916759.html