当前位置:   article > 正文

Unity-Json数据解析_unity json解析

unity json解析

一、Json序列化(写入Json)

先附上Litjson.dll网盘地址(提取码:kkkk)https://pan.baidu.com/s/102xhr6O_LKeMmO7LJlLT0Q
命名空间引入

using UnityEngine;
using LitJson;
using System.IO;
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

定义一个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();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

在Unity中运行后效果如下:
在这里插入图片描述

二、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":"她单身"}
	]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
    /// <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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

最终在Unityi中运行发现正常读取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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/123409
推荐阅读
相关标签
  

闽ICP备14008679号