unity — Json解析

注:利用LitJson进行解析

 

创建一个AppOfTriggerPhone类:

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

在创建Assets下创建Resources文件夹下创建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;
    }

}

再在Start方法中读取:

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

 

复杂的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;
    }

}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值