Unity json 写入与解析 自带的 JsonUtility

这篇博客展示了如何在Unity中使用JsonUtility类进行数据对象与JSON字符串之间的转换。作者创建了两个类mPerson和Player,并填充了实例数据,然后通过JsonUtility将对象转换为JSON字符串进行日志打印。接着,又将JSON字符串反序列化回Player对象,进一步验证了转换的正确性。此示例适用于理解Unity中的JSON操作和数据序列化。
摘要由CSDN通过智能技术生成
public class mPerson {
    public string name;
    public int age;
    public string ID;
    public string[] arr;
    List<int> array;
    public void Sex() { }

}
class Player 
{
    public string name;
    public int age;
    public string ID;
    public string[] arr;
    List<int> array;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Xml;
public  class JsonTools :Editor
{
    [MenuItem("Mytools/JSONRun")]
    public static void JsonRun() {
        mPerson mp = new mPerson();
        mp.name = "张三";
        mp.ID = "123456";
        mp.age = 18;
        mp.arr=  new string[] { "asd", "sdf", "sdf", "asd", "aasdf" };
        List<int> array = new List<int>();
        array.Add(1);
        array.Add(2);
        array.Add(3);
        array.Add(4);
        array.Add(5);
        array.Add(6);
        array.Add(7);
        array.Add(8);
        string str = JsonUtility.ToJson(mp);
        Debug.Log(str);
        Player p=  JsonUtility.FromJson<Player>(str);
        Debug.Log(p.name);
        Debug.Log(p.age);
        Debug.Log(p.ID);
        Debug.Log(p.arr[0]);
        Debug.Log(p.arr[1]);
        Debug.Log(p.arr[2]);
        Debug.Log(p.arr[3]);
        Debug.Log(p.arr[4]);
        Debug.Log(array[0]);
        Debug.Log(array[1]);
        Debug.Log(array[2]);
        Debug.Log(array[3]);
        Debug.Log(array[4]);
        Debug.Log(array[5]);
        Debug.Log(array[6]);
        Debug.Log(array[7]);
      
    }
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值