栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

Unity——写入和读取Json信息的方法

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity——写入和读取Json信息的方法

 先定义一个类,假如现在是要查看手机里APP的信息,把信息先放进去(这里分为简单和复杂读取时的情况,测试的时候要先把另一个注释掉!)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class APPofMine
{
    public int appNum;

    public bool phoneState;

    //简单写入和读取的时候直接用List来保存数据
    //public List appList;


    //复杂写入和读取的时候定义一个List存放另一个类里的数据
    public List appPropertiesList;
}

另一个类里的数据: 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppProperty
{
    public string appName;
    public string ErenID;
    public bool ErenFavour;
    public List useTimeList;
}

写入和读取的方法及调用测试代码:

代码比较长,有详细的注释!可以直接全段复制~

PS:测试时要先在Unity创建一个物体,然后把此脚本挂在物体下,另外还需要在Assets目录下创建一个 Resources 文件夹,再在Resources 文件夹下创建一个Json文件夹!!!

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


public class JosnTest : MonoBehaviour
{
    private APPofMine appOfMine;

    // Start is called before the first frame update
    void Start()
    {
        #region 简单Json信息的存储和读取

        //简单Json信息的存储和读取
        
        #endregion


        #region 复杂Json信息写入和读取
        写入
        //appOfMine = new APPofMine
        //{
        //    appNum = 3,
        //    phoneState = true,
        //    appPropertiesList = new List()
        //};

        //AppProperty appProperty = new AppProperty
        //{
        //    appName = "抖音",
        //    ErenID = "冬瓜大浪",
        //    ErenFavour = true,
        //    useTimeList = new List { 6, 7, 8 }
        //};
        //appOfMine.appPropertiesList.Add(appProperty);
        //SaveByJson();

        //读取
        appOfMine = LoadByJson();
        print(appOfMine.appNum);
        print(appOfMine.phoneState);
        foreach (var item in appOfMine.appPropertiesList)
        {
            print(item);
            print(item.appName);
            print(item.ErenFavour);
            print(item.ErenID);
            foreach (var itemGo in item.useTimeList)
            {
                print(itemGo);
            }
        }
        #endregion
    }


    #region 简单存储Json信息文件的方法
    //存储Json信息文件
    private void SaveByJson()
    {
        //找到文件要存储的路径
        string filePath = Application.dataPath + "/Resources" + "/APPofMine.json";

        //利用JsonMapper将 信息类对象(字段名) 转化成 json格式 的 字符串
        string saveJsonStr = JsonMapper.ToJson(appOfMine);

        //创建一个文件流将字符串写入一个文件中
        StreamWriter sw = new StreamWriter(filePath);

        //开始写入
        sw.Write(saveJsonStr);

        //关闭文件流
        sw.Close();
    }
    #endregion


    #region 简单读取Json的信息文件
    //读取Json的信息文件
    private APPofMine LoadByJson()
    {
        APPofMine appGo = new APPofMine();

        //找到需要读取文件的路径
        string filePath = Application.dataPath + "/Resources" + "/APPofMine.json";

        //先判断如果文件夹存在
        if (File.Exists(filePath))
        {
            //创建一个新的变量 sr ,用来存储读取到的数据,开始读取
            StreamReader sr = new StreamReader(filePath);

            //把读取到的信息转换成字符串的形式存储下来
            string loadJsonStr = sr.ReadToEnd();

            //关闭文件流
            sr.Close();

            //存储读取到的数据
            appGo = JsonMapper.ToObject(loadJsonStr);
        }

        //判空
        if (appGo == null)
        {
            Debug.Log("读取Json文件失败");
        }

        return appGo;
    }
    #endregion






}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号