Unity JSON 序列化

这篇博客探讨了在Unity环境下如何利用JSON进行对象序列化。内容包括测试类的运用,介绍了各种JSON工具,并提到了一个大型的JSON.cs类,该类文件可在指定的优快云下载链接中获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试 类

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class App : MonoBehaviour
{
    PlayInfo info; 
    public void Awake()
    {

        ToJson();
        FromJson();

    }
    // 序列化
    void ToJson()
    {
        info = new PlayInfo("Unity", "123456", 234, "Json");
        string infoJson = JsonTool.ToJson(info);
        string path = FileHelp.PersistentWrite(info.AccountName + ".txt", infoJson);
        Debug.Log(path);
    }
    //反序列化
    void FromJson()
    {
        info = new PlayInfo();
        info.DeSerialize(JsonTool.DictionaryFromJson(FileHelp.PersistentLoadOrReard("Unity.txt")));
        Debug.Log(info.AccountName);
    }
}

序列化对象

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

[System.Serializable]
public class PlayInfo  {

    public string AccountName;

    public string PassWord;

    public int Gold;

    public string NickName;

    public string Grade;

    public int Score;

    public PlayInfo()
    {

    }
    public PlayInfo(string accountName,string password,int gold,string nickName,string grade = "初级",int score = 0)
    {
        this.AccountName = accountName;
        this.PassWord = password;
        this.Gold = gold;
        this.NickName = nickName;
        this.Grade = grade;
        this.Score = score;
    }
    //反序列化
    public  void DeSerialize(Dictionary<string ,object> obj)
    {
        foreach (string  item in obj.Keys)
        {
            switch (item)
            {
                case "AccountName":
                    AccountName = JsonTool.GetStringValue(obj,item);
                    break;
                case "PassWord":
                    PassWord = JsonTool.GetStringValue(obj, item);
                    break;
                case "Gold":
                    Gold = JsonTool.GetIntValue(obj, item);
                    break;
                case "NickName":
                    NickName = JsonTool.GetStringValue(obj, item);
                    break;
                case "Grade":
                    Grade = JsonTool.GetStringValue(obj, item);
                    break;
                case "Score":
                    Score = JsonTool.GetIntValue(obj, item);
                    break;
                default:
                    break;
            }
        }
    }
}

JSON 工具

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

public static class JsonTool   {

    public static string ToJson(object obj)
    {
        return Json.Serialize(obj);
    }
    public static object FromJson(string str)
    {
        return Json.Deserialize(str);
    }
    public static Dictionary<string, object> DictionaryFromJson(string json)
    {
        return (Dictionary<string, object>)Json.Deserialize(json);
    }
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="dic">字典</param>
    /// <param name="key">字典里具体键</param>
    /// <returns></returns>
    public static int GetIntValue(Dictionary<string, object> dic, string key)
    {
        object value = GetValue(dic, key);
        if (value == null)
        {
            return 0;
        }
        int intValue;
        if (int.TryParse(value.ToString(), out intValue) == false)
        {
            Debug.Log(int.Parse(value.ToString()));
            Debug.LogError("Value 转换失败 " + intValue);
        }
        return intValue;
    }
    public static string GetStringValue(Dictionary<string, object> dic, string key)
    {
        object value = GetValue(dic, key);

        if (value == null)
        {
            return null;
        }
        return value.ToString();
    }
    public static object GetValue(Dictionary<string, object> dic, string key)
    {
        if (dic == null)
        {
            Debug.LogError("Dictionary == null");
            return null;
        }
        object value;
        if (dic.TryGetValue(key, out value))
        {
            return value;
        }
        Debug.LogError("Key not reference");
        return null;

    }
}

还有一个 JSON.cs 类过大,存放在工程里。
工程地址:http://download.youkuaiyun.com/detail/microsoftmsdnnet/9626477

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值