zoukankan      html  css  js  c++  java
  • Json格式应用

    Json格式在用于数据存储方面比xml有着空间上的优势,Json格式又主要分为两种格式:名称/值 对 和数组。

    在我的业务环境中需要先把一种空间比较小的格式。

    测试如下:

    取数据库中的一张表然后生成两种格式的Json文件进行对比。

    生成名称/值 对的格式,样式如下:

    大小为:6460KB

    生成数组的格式,样式如下:

    大小为:2736KB。

    数组由于列的信息只显示一次,大小约为原来的50%。

    生成两种格式的C#代码如下:

        static void Main(string[] args)
            {
                DataTable dt = DataAccess.GetData("select * from stk_code");
    
                string sJson= JsonConvert.SerializeObject(dt);
                FileStream fs = new FileStream("D:\Json.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(sJson);
                sw.Close();
                fs.Close();
    
                Dictionary<string, List<object>> dataList = DataToList.DataTableToList(dt);
                sJson = JsonConvert.SerializeObject(dataList);
                fs = new FileStream("D:\Json2.txt", FileMode.Append);
                sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(sJson);
                sw.Close();
                fs.Close();
    
    
                Console.ReadKey();
            }

    将Datatable转为Dictionary 的方法如下:

      public class DataToList
        {
            /// <summary>
            /// 将datarow 转化为Dictionary<string, List<object>>
            /// 结果集中第一行为列名,第二行开始为数据,数据行用数字0-N表示
            /// </summary>
            /// <param name="table"></param>
            /// <returns></returns>
            public static Dictionary<string, List<object>> DataTableToList(DataTable table)
            {
                Dictionary<string, List<object>> DataList = new Dictionary<string, List<object>>();
    
                List<object> itemValue = new List<object>();
                string itemKey = "col";
    
                foreach (DataColumn dc in table.Columns)
                {
                    itemValue.Add(dc.ColumnName);
                }
                DataList.Add(itemKey, itemValue);
    
                int i = 0;
                foreach (DataRow dr in table.Rows)
                {
                    itemKey = i.ToString();
                    itemValue = new List<object>();
    
                    foreach (var val in dr.ItemArray)
                    {
                        itemValue.Add(val);
                    }
                    DataList.Add(itemKey, itemValue);
                    i++;
                }
    
                return DataList;
            }
    
        }


    //写流文件
    ObjectStream.Serialize<Dictionary<string, List<object>>>(dataList, @"D:stream.bin");

    
    

    //读流文件
    Dictionary<string, List<object>> ndt = (Dictionary<string, List<object>>)ObjectStream.Deserialize(@"D:stream.bin");

     

    数组的格式虽然占用的空间会小一些,但需要进行一次转换,所以在选择的时候需要视具体情况而定。

    主要有两方面的因素:

    1.列名和内容的比例大小关系,如果大部分字段是文本类的,大小的区别应该不大。采用数组反而多一次转换。

    2.是带宽为稀缺资源还是CPU为稀缺资源?

    第三种方案,将数据对象直接存储为流:

    如这个对象是DataTable则大小为:10368KB。如采用简单一点的对象如

    Dictionary<string,List<object>>则大小为:3225

    所以大小取决于对象的简单程度。

    对象和流之间的转换代码:

      public class ObjectStream
        {
            /// <summary>
            /// 将一个对象转换为流文件
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <param name="filename"></param>
            /// <returns></returns>
            public static bool Serialize<T>(T value, string filename)
            {
                try
                {
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    System.IO.FileStream stream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                    bs.Serialize(stream, value);
                    stream.Close();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 从流文件中获取一个对象
            /// </summary>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public static Object Deserialize(string fileName)
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bs = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
                return bs.Deserialize(stream);
            }
        }
  • 相关阅读:
    日志
    设置和开启定时器
    缓存管理
    计算机程序员能做多久,这个行业有年龄限制吗?
    程序员都是怎么工作的?
    做程序员怎么样?
    javascript中this关键字
    1003. 二哥养细菌—java
    1002. 二哥种花生——java
    this与static
  • 原文地址:https://www.cnblogs.com/champaign/p/6223351.html
Copyright © 2011-2022 走看看