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

Unity-Json解析

程序员文章站 2022-03-28 08:21:40
...

一、Json序列化(写入Json)

命名空间引入

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

定义一个JsonInfo类,在对此类进行赋值之后进行json序列化操作

    /// <summary>
    /// 文件路径
    /// </summary>
    private string filePath = "C:/david.json";

    private void Start()
    {
        WriteJson();
    }
    /// <summary>
    /// Json信息类
    /// </summary>
    public class JsonInfo
    {
        public string userName;
    }

    /// <summary>
    /// Json写入
    /// </summary>
    private void WriteJson()
    {
        //初始化
        JsonInfo jsonInfo = new JsonInfo();
        jsonInfo.userName = "小华";

        StreamWriter sw = new StreamWriter(filePath);
        string json = JsonMapper.ToJson(jsonInfo);

        //转码 
        Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
        json = reg.Replace(json, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });

        //写入信息
        sw.Write(json);
        sw.Dispose();
        sw.Close();
    }

在Unity中运行后效果如下:
Unity-Json解析

二、Json反序列化(读取Json)

相对于写入,读取应用的更加广泛一些,其作用和我之前分享的Unity与Excel表格交互类似。
包含了Json文件,字段、类、数组的读取(在C盘新建.json文件,用记事本或VS打开)。

{
	"userName":"小华",

	"userData":
	{
		"name":"小明",
		"age":25
	},

	"studentInfo":
	[
		{"name":"贝贝","age":20,"description":"爱吃萝卜和青菜"},
		{"name":"晶晶","age":16,"description":"喜欢健身"},
		{"name":"妮妮","age":29,"description":"她单身"}
	]
}
    /// <summary>
    /// Json读取
    /// </summary>
    private void ReadJson()
    {
        //文件流 读取
        StreamReader sr = new StreamReader(filePath);

        //读取的Json字符串
        string readContent = sr.ReadToEnd();

        //反序列化操作
        JsonInfo json = JsonMapper.ToObject<JsonInfo>(readContent);
        Debug.Log("userName:" + json.userName);
        Debug.Log("UserData name:" + json.userData.name);
        Debug.Log("UserData age:" + json.userData.age);
        Debug.Log("StudentInfo 0 name:" + json.studentInfo[0].name);
        Debug.Log("StudentInfo 0 age:" + json.studentInfo[0].age);
        Debug.Log("StudentInfo 0 descrption:" + json.studentInfo[0].description);
    }

    /// <summary>
    /// Json信息类
    /// </summary>
    public class JsonInfo
    {
        public string userName;
        public UserData userData;
        public List<StudentInfo> studentInfo;
    }

    /// <summary>
    /// 测试 用户数据类
    /// </summary>
    public class UserData
    {
        public string name;
        public int age;
    }

    /// <summary>
    /// 测试 学生信息类
    /// </summary>
    public class StudentInfo
    {
        public string name;
        public int age;
        public string description;
    }

最终在Unityi中运行发现正常读取json文件
Unity-Json解析
完整代码如下:

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

public class JsonTest : MonoBehaviour
{
    /// <summary>
    /// 文件路径
    /// </summary>
    private string filePath = "C:/david.json";

    private void Start()
    {
        //WriteJson();
        ReadJson();
    }

    /// <summary>
    /// Json写入
    /// </summary>
    private void WriteJson()
    {
        //初始化
        JsonInfo jsonInfo = new JsonInfo();
        jsonInfo.userName = "小华";

        StreamWriter sw = new StreamWriter(filePath);
        string json = JsonMapper.ToJson(jsonInfo);

        //转码 
        Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
        json = reg.Replace(json, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });

        //写入信息
        sw.Write(json);
        sw.Dispose();
        sw.Close();
    }

    /// <summary>
    /// Json读取
    /// </summary>
    private void ReadJson()
    {
        //文件流 读取
        StreamReader sr = new StreamReader(filePath);

        //读取的Json字符串
        string readContent = sr.ReadToEnd();

        //反序列化操作
        JsonInfo json = JsonMapper.ToObject<JsonInfo>(readContent);
        Debug.Log("userName:" + json.userName);
        Debug.Log("UserData name:" + json.userData.name);
        Debug.Log("UserData age:" + json.userData.age);
        Debug.Log("StudentInfo 0 name:" + json.studentInfo[0].name);
        Debug.Log("StudentInfo 0 age:" + json.studentInfo[0].age);
        Debug.Log("StudentInfo 0 descrption:" + json.studentInfo[0].description);
    }

    /// <summary>
    /// Json信息类
    /// </summary>
    public class JsonInfo
    {
        public string userName;
        public UserData userData;
        public List<StudentInfo> studentInfo;
    }

    /// <summary>
    /// 测试 用户数据类
    /// </summary>
    public class UserData
    {
        public string name;
        public int age;
    }

    /// <summary>
    /// 测试 学生信息类
    /// </summary>
    public class StudentInfo
    {
        public string name;
        public int age;
        public string description;
    }
}
相关标签: 文件读写