unity json序列化和反序列化

using Newtonsoft.Json;
using System.Collections.Generic;
using UnityEngine;

public class JsonDe : MonoBehaviour
{
    public class Produc
    {
        public string name = "sdf";
        public int a = 1;
        public bool b = true;
        public float c = 2.1f;
        public Dictionary<string, string> dic = new Dictionary<string, string> { { "sdfsd", "654" }, { "dfsdf", "354" } };
        public string[] ss = { "sdfsdf", "3213", "sdfsdf" };
        public int[,] ves = new int[1, 2] { { 1, 2 } };

    }
    
    void Start()
    {
        Produc produc = new Produc();

        string json = JsonConvert.SerializeObject(produc);

        Debug.Log(json);

        Produc m = JsonConvert.DeserializeObject<Produc>(json);

        Debug.Log(m.ves[0, 0]);

    }

   
}
View Code

需要用到Newtonsoft.Json.dll, 放到Plugins文件夹下面 

csdn下载链接  https://download.csdn.net/download/gp1320099322/11932181

猜你喜欢

转载自www.cnblogs.com/G993/p/11751818.html