zoukankan      html  css  js  c++  java
  • JsonFX 序列化反序列化

    /// <summary>
    /// Json 序列化工具类
    /// </summary>
    public class SerializeJsonUtility
    {
        /// <summary>
        /// 根据一个JSON,得到一个类
        /// </summary>
        public static T JsonToClass<T>(string json) where T : class
        {
            T t = JsonReader.Deserialize<T>(json);
            return t;
        }
    
        /// <summary>
        /// 根据一个JSON的文件Resources地址,得到一个类
        /// </summary>
        public static T AddressToClass<T>(string txtAddress) where T : class
        {
            TextAsset jsonData = Resources.Load(txtAddress) as TextAsset;
            return JsonToClass<T>(jsonData.text);
        }
    
        /// <summary>
        /// 将JSON转换为一个类数组
        /// </summary>
        public static T[] JsonToClasses<T>(string json) where T : class
        {
            //Debug.Log(json);
            T[] list = JsonReader.Deserialize<T[]>(json);
            return list;
        }
    
        /// <summary>
        /// 将JSON转换为一个类集合
        /// </summary>
        public static List<T> JsonToClasseList<T>(string json) where T : class
        {
            T[] ary = JsonReader.Deserialize<T[]>(json);
            List<T> list = new List<T>();
            if (ary!=null && ary.Length >0)
            {
                for (int i = 0; i < ary.Length; i++)
                {
                    list.Add(ary[i]);
                }
            }
            return list;
        }
    
        /// <summary>
        /// 给Json文件的Resources地址。转换为一个类数组
        /// </summary>
        public static T[] AddressToClasses<T>(string txtAddress) where T : class
        {
            TextAsset jsonData = Resources.Load(txtAddress) as TextAsset;
            return JsonToClasses<T>(jsonData.text);
        }
    }
  • 相关阅读:
    java实现HTTP请求 HttpUtil
    java-websocket客户端 断线重连 注入Service问题
    人工智能博客
    git 修改注释
    2019-2-22
    2019-2-21
    2019-2-20
    /与./和../的含义
    第二章(构建有多个房间的聊天室程序)
    第一章(欢迎进入node.js世界)
  • 原文地址:https://www.cnblogs.com/123ing/p/3779815.html
Copyright © 2011-2022 走看看