欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity — Json解析

程序员文章站 2022-03-28 11:06:19
...

创建一个AppOfTriggerPhone类:

public class AppOfTriggerPhone
{
    public int appNum;
    public bool PhoneState;
    public List<string> appList;
}

在创建Assets下创建Resources文件夹下创建Json文件夹

如:unity — Json解析

创建JsonTest脚本,声明写入和读取方法:

在Start中写入Json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class JsonText : MonoBehaviour
{
    private AppOfTriggerPhone appOfTriggerPhone;

    private void Start()
    {
        //  简单的Json解析
        appOfTriggerPhone = new AppOfTriggerPhone()
        {
            appNum = 3,
            PhoneState = true,
            appList = new List<string>()
           {
            "抖音","Bilibili","绝地求生"
           }
        };

        SaveByJson();
    }

    public void SaveByJson()
    {
        string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";

        string saveJsonStr = JsonMapper.ToJson(appOfTriggerPhone);

        StreamWriter sw = new StreamWriter(filePath);
        sw.Write(saveJsonStr);
        sw.Close();
    }

    public AppOfTriggerPhone LoadByJson()
    {
        AppOfTriggerPhone appGO = new AppOfTriggerPhone();
        string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";
        if (File.Exists(filePath))
        {
            StreamReader sr = new StreamReader(filePath);
            string jsonStr = sr.ReadToEnd();
            sr.Close();
            appGO = JsonMapper.ToObject<AppOfTriggerPhone>(jsonStr);
        }
        if (appGO == null)
        {
            Debug.Log("读取Json信息失败");
        }
        return appGO;
    }

}

unity — Json解析

再在Start方法中读取:

 appOfTriggerPhone = new AppOfTriggerPhone();
        appOfTriggerPhone = LoadByJson();
        Debug.Log(appOfTriggerPhone.appNum);
        Debug.Log(appOfTriggerPhone.PhoneState);
        foreach (var item in appOfTriggerPhone.appList)
        {
            Debug.Log(item);
        }

 

unity — Json解析

复杂的Json读取:

创建新的类AppProperty,并修改AppOfTriggerPhone

public class AppProperty  {

    public string appName;
    public string triggerID;
    public bool triggerFavour;
    public List<int> useTimeList;
}


public class AppOfTriggerPhone
{
    public int appNum;
    public bool PhoneState;
    public List<AppProperty> appList;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;

public class JsonText : MonoBehaviour
{
    private AppOfTriggerPhone appOfTriggerPhone;

    private void Start()
    {

        //复杂的Json解析
        appOfTriggerPhone = new AppOfTriggerPhone
        {
            appNum = 3,
            PhoneState = true,
            appList = new List<AppProperty>()
        };
        AppProperty appProperty = new AppProperty
        {
            appName = "抖音",
            triggerID = "Trigger",
            triggerFavour = true,
            useTimeList = new List<int> { 6, 7, 8 }
        };
        appOfTriggerPhone.appList.Add(appProperty);
        SaveByJson();

        appOfTriggerPhone = LoadByJson();
        Debug.Log(appOfTriggerPhone.appNum);
        Debug.Log(appOfTriggerPhone.PhoneState);
        foreach (var item in appOfTriggerPhone.appList)
        {
            Debug.Log(item);
            Debug.Log(item.appName);
            Debug.Log(item.triggerFavour);
            Debug.Log(item.triggerID);
            foreach (var itemGo in item.useTimeList)
            {
                Debug.Log(itemGo);
            }
        }
    }

    public void SaveByJson()
    {
        string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";

        string saveJsonStr = JsonMapper.ToJson(appOfTriggerPhone);

        StreamWriter sw = new StreamWriter(filePath);
        sw.Write(saveJsonStr);
        sw.Close();
    }

    public AppOfTriggerPhone LoadByJson()
    {
        AppOfTriggerPhone appGO = new AppOfTriggerPhone();
        string filePath = Application.dataPath + "/Resources/Json" + "/AppOfTrigger.json";
        if (File.Exists(filePath))
        {
            StreamReader sr = new StreamReader(filePath);
            string jsonStr = sr.ReadToEnd();
            sr.Close();
            appGO = JsonMapper.ToObject<AppOfTriggerPhone>(jsonStr);
        }
        if (appGO == null)
        {
            Debug.Log("读取Json信息失败");
        }
        return appGO;
    }

}

运行结果:

unity — Json解析