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

首先在Unity的Assets目录下新建一个Plugins文件夹,引入LitJson.dll类库!!!

编写读写脚本时,还需要再导入命名空间 using LitJson

关于Litjson文件:

可以去官网下载一个文件包;.dll文件存在于  litjson-0.5.0/bin目录。

 Litjson官网下载链接LitJSON download | SourceForge.net

具体操作:

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

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

public class APPofMine
{
    public int appNum;

    public bool phoneState;

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


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

另一个类里的数据: 

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

public class AppProperty
{
    public string appName;
    public string ErenID;
    public bool ErenFavour;
    public List<int> 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信息的存储和读取
        /*
        写入
        //appOfMine = new APPofMine
        //{
        //    appNum = 3,
        //    phoneState = true,
        //    appList = new List<string>()
        //    {
        //        "抖音","BiliBili","绝地求生"
        //    }
        //};

        //SaveByJson();

        //读取
        appOfMine = new APPofMine();

        appOfMine = LoadByJson();

        print(appOfMine.appNum);
        print(appOfMine.phoneState);
        foreach (var item in appOfMine.appList)
        {
            print(item);
        }
        */
        #endregion


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

        //AppProperty appProperty = new AppProperty
        //{
        //    appName = "抖音",
        //    ErenID = "冬瓜大浪",
        //    ErenFavour = true,
        //    useTimeList = new List<int> { 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/*Assets根目录*/ + "/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/*Assets根目录*/ + "/Resources"/*后续路径*/ + "/APPofMine.json"/*文件名*/;

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

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

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

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

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

        return appGo;
    }
    #endregion






}

最近工作中遇到一个需求,后端的一个接口的参数是List,List中的元素又是数组,相当于二维数组。但是二维数组无法直接转成Json格式传递。 

      搞了好一会儿,记录一下方法:

        JsonData addressInfo = new JsonData();//创建一个addressInfo对象
        foreach (var item in companyInfo.address)
        {
            JsonData dataArray = new JsonData();//在addressInfo中再创建一个对象
            dataArray["id"] = item.id;
            dataArray["address"] = item.address;
            addressInfo.Add(dataArray);//依次添加进addressInfo
        }
        jd["address"] = addressInfo;//赋值

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值