JsonOperate 帮助类

引用 Newtonsoft.Json

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization;

//下载Newtonsoft.Json.dll
public class JsonOperate
{
    private JavaScriptSerializer Jss = new JavaScriptSerializer();

    private string _IdField;

    private string _IdType;

    private string _TextField;

    private string _StateField;

    private string _CheckedField;

    private string _ParentIdField;

    private string _IconClsField;

    private string _AttributesField;

    private string _IconCls;

    private string _ParentId;

    private string _State;

    private string _AllData;

    private string _Checked;

    private string _Attributes;

    private DataTable _JsonTable;

    public DataTable JsonTable
    {
        get
        {
            return this._JsonTable;
        }
        set
        {
            this._JsonTable = value;
        }
    }

    public string IdField
    {
        get
        {
            return this._IdField;
        }
        set
        {
            this._IdField = value;
        }
    }

    public string IdType
    {
        get
        {
            return this._IdType;
        }
        set
        {
            this._IdType = value;
        }
    }

    public string TextField
    {
        get
        {
            return this._TextField;
        }
        set
        {
            this._TextField = value;
        }
    }

    public string StateField
    {
        get
        {
            return this._StateField;
        }
        set
        {
            this._StateField = value;
        }
    }

    public string CheckedField
    {
        get
        {
            return this._CheckedField;
        }
        set
        {
            this._CheckedField = value;
        }
    }

    public string SelectedField
    {
        get;
        set;
    }

    public string ParentId
    {
        get
        {
            return this._ParentId;
        }
        set
        {
            this._ParentId = value;
        }
    }

    public string ParentIdField
    {
        get
        {
            return this._ParentIdField;
        }
        set
        {
            this._ParentIdField = value;
        }
    }

    public string AttributesField
    {
        get
        {
            return this._AttributesField;
        }
        set
        {
            this._AttributesField = value;
        }
    }

    public string IconClsField
    {
        get
        {
            return this._IconClsField;
        }
        set
        {
            this._IconClsField = value;
        }
    }

    public string IconCls
    {
        get
        {
            return this._IconCls;
        }
        set
        {
            this._IconCls = value;
        }
    }

    public string Attributes
    {
        get
        {
            return this._Attributes;
        }
        set
        {
            this._Attributes = value;
        }
    }

    public string Checked
    {
        get
        {
            return this._Checked;
        }
        set
        {
            this._Checked = value;
        }
    }

    public string State
    {
        get
        {
            return this._State;
        }
        set
        {
            this._State = value;
        }
    }

    public string AllData
    {
        get
        {
            return this._AllData;
        }
        set
        {
            this._AllData = value;
        }
    }

    public string GetTreeJson()
    {
        System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
        string text = "";
        if (!string.IsNullOrEmpty(this.ParentIdField))
        {
            text = this.ParentIdField + " is Null";
            if (string.IsNullOrEmpty(this.IdType))
            {
                text = text + " or " + this.ParentIdField + "=‘0‘";
            }
            else
            {
                text = text + " or " + this.ParentIdField + "=‘‘";
            }
        }
        if (this.AllData == "true")
        {
            list.Add(new System.Collections.Generic.Dictionary<string, object>
                {
                    {
                        "id",
                        ""
                    },
                    {
                        "text",
                        "全部"
                    }
                });
        }
        DataRow[] array = this.JsonTable.Select(text);
        for (int i = 0; i < array.Length; i++)
        {
            DataRow dataRow = array[i];
            System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
            this.ParentId = dataRow[this.IdField].ToString();
            dictionary.Add("id", dataRow[this.IdField].ToString());
            dictionary.Add("text", dataRow[this.TextField].ToString());
            if (!string.IsNullOrEmpty(this.StateField))
            {
                dictionary.Add("state", dataRow[this.StateField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.State))
            {
                dictionary.Add("state", this.State);
            }
            if (!string.IsNullOrEmpty(this.CheckedField))
            {
                dictionary.Add("checked", dataRow[this.CheckedField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Checked))
            {
                dictionary.Add("checked", this.Checked);
            }
            if (!string.IsNullOrEmpty(this.SelectedField))
            {
                dictionary.Add("selected", dataRow[this.SelectedField].ToString());
            }
            if (!string.IsNullOrEmpty(this.IconClsField))
            {
                dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.IconCls))
            {
                dictionary.Add("iconCls", this.IconCls);
            }
            if (!string.IsNullOrEmpty(this.AttributesField))
            {
                dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Attributes))
            {
                dictionary.Add("attributes", this.Attributes);
            }
            if (!string.IsNullOrEmpty(this.ParentIdField))
            {
                dictionary.Add("children", this.GetTreeChildrenJson());
            }
            list.Add(dictionary);
        }
        return this.Jss.Serialize(list);
    }

    public System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> GetTreeChildrenJson()
    {
        System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
        string filterExpression = this.ParentIdField + " = ‘" + this.ParentId + "‘";
        DataRow[] array = this.JsonTable.Select(filterExpression);
        for (int i = 0; i < array.Length; i++)
        {
            DataRow dataRow = array[i];
            System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
            this.ParentId = dataRow[this.IdField].ToString();
            dictionary.Add("id", dataRow[this.IdField].ToString());
            dictionary.Add("text", dataRow[this.TextField].ToString());
            if (!string.IsNullOrEmpty(this.StateField))
            {
                dictionary.Add("state", dataRow[this.StateField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.State))
            {
                dictionary.Add("state", this.State);
            }
            if (!string.IsNullOrEmpty(this.CheckedField))
            {
                dictionary.Add("checked", dataRow[this.CheckedField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Checked))
            {
                dictionary.Add("checked", this.Checked);
            }
            if (!string.IsNullOrEmpty(this.SelectedField))
            {
                dictionary.Add("selected", dataRow[this.SelectedField].ToString());
            }
            if (!string.IsNullOrEmpty(this.IconClsField))
            {
                dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.IconCls))
            {
                dictionary.Add("iconCls", this.IconCls);
            }
            if (!string.IsNullOrEmpty(this.AttributesField))
            {
                dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Attributes))
            {
                dictionary.Add("attributes", this.Attributes);
            }
            dictionary.Add("children", this.GetTreeChildrenJson());
            list.Add(dictionary);
        }
        return list;
    }

    public string GetTreeGridJson()
    {
        System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
        string text = this.ParentIdField + " is Null";
        if (string.IsNullOrEmpty(this.IdType))
        {
            text = text + " or " + this.ParentIdField + "=‘0‘";
        }
        else
        {
            text = text + " or " + this.ParentIdField + "=‘‘";
        }
        DataRow[] array = this.JsonTable.Select(text);
        for (int i = 0; i < array.Length; i++)
        {
            DataRow dataRow = array[i];
            System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
            foreach (DataColumn dataColumn in this.JsonTable.Columns)
            {
                if (dataColumn.DataType == System.Type.GetType("System.DateTime"))
                {
                    string value;
                    if (string.IsNullOrEmpty(dataRow[dataColumn.ColumnName].ToString()))
                    {
                        value = "";
                    }
                    else
                    {
                        value = System.Convert.ToDateTime(dataRow[dataColumn.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm");
                    }
                    dictionary.Add(dataColumn.ColumnName, value);
                }
                else
                {
                    dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
                }
            }
            this.ParentId = dataRow[this.IdField].ToString();
            if (!string.IsNullOrEmpty(this.StateField))
            {
                dictionary.Add("state", dataRow[this.StateField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.State))
            {
                dictionary.Add("state", this.State);
            }
            if (!string.IsNullOrEmpty(this.CheckedField))
            {
                dictionary.Add("checked", dataRow[this.CheckedField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Checked))
            {
                dictionary.Add("checked", this.Checked);
            }
            if (!string.IsNullOrEmpty(this.IconClsField))
            {
                dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.IconCls))
            {
                dictionary.Add("iconCls", this.IconCls);
            }
            if (!string.IsNullOrEmpty(this.AttributesField))
            {
                dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Attributes))
            {
                dictionary.Add("attributes", this.Attributes);
            }
            dictionary.Add("children", this.GetTreeGridChildrenJson());
            list.Add(dictionary);
        }
        return this.Jss.Serialize(list);
    }

    public System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> GetTreeGridChildrenJson()
    {
        System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
        string filterExpression = this.ParentIdField + " = ‘" + this.ParentId + "‘";
        DataRow[] array = this.JsonTable.Select(filterExpression);
        for (int i = 0; i < array.Length; i++)
        {
            DataRow dataRow = array[i];
            System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
            foreach (DataColumn dataColumn in this.JsonTable.Columns)
            {
                if (dataColumn.DataType == System.Type.GetType("System.DateTime"))
                {
                    string value;
                    if (string.IsNullOrEmpty(dataRow[dataColumn.ColumnName].ToString()))
                    {
                        value = "";
                    }
                    else
                    {
                        value = System.Convert.ToDateTime(dataRow[dataColumn.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm");
                    }
                    dictionary.Add(dataColumn.ColumnName, value);
                }
                else
                {
                    dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
                }
            }
            this.ParentId = dataRow[this.IdField].ToString();
            if (!string.IsNullOrEmpty(this.StateField))
            {
                dictionary.Add("state", dataRow[this.StateField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.State))
            {
                dictionary.Add("state", this.State);
            }
            if (!string.IsNullOrEmpty(this.CheckedField))
            {
                dictionary.Add("checked", dataRow[this.CheckedField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Checked))
            {
                dictionary.Add("checked", this.Checked);
            }
            if (!string.IsNullOrEmpty(this.IconClsField))
            {
                dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.IconCls))
            {
                dictionary.Add("iconCls", this.IconCls);
            }
            if (!string.IsNullOrEmpty(this.AttributesField))
            {
                dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
            }
            else if (!string.IsNullOrEmpty(this.Attributes))
            {
                dictionary.Add("attributes", this.Attributes);
            }
            dictionary.Add("children", this.GetTreeGridChildrenJson());
            list.Add(dictionary);
        }
        return list;
    }

    public string GetDataGridJson(DataTable JsonTable)
    {
        return string.Concat(new string[]
            {
                "{ \"total\":",
                JsonTable.Rows.Count.ToString(),
                ",\"rows\":",
                this.TableToBasicJson(JsonTable),
                "}"
            });
    }

    public string GetDataGridJson(DataTable JsonTable, string strCount)
    {
        return string.Concat(new string[]
            {
                "{ \"total\":",
                strCount,
                ",\"rows\":",
                this.TableToBasicJson(JsonTable),
                "}"
            });
    }

    public string GetDataGridJson(DataTable JsonTable, int count)
    {
        return string.Concat(new object[]
            {
                "{ \"total\":",
                count,
                ",\"rows\":",
                this.TableToBasicJson(JsonTable),
                "}"
            });
    }

    public string GetDataGridJson(DataTable JsonTable, DataTable strCount, DataTable FooterTable)
    {
        return string.Concat(new object[]
            {
                "{ \"total\":",
                strCount,
                ",\"rows\":",
                this.TableToBasicJson(JsonTable),
                ",\"footer\":",
                this.TableToBasicJson(FooterTable),
                "}"
            });
    }

    public string TableToBasicJson(DataTable JsonTable)
    {
        System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
        foreach (DataRow dataRow in JsonTable.Rows)
        {
            System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
            foreach (DataColumn dataColumn in JsonTable.Columns)
            {
                if (dataColumn.DataType == System.Type.GetType("System.DateTime"))
                {
                    string value;
                    if (string.IsNullOrEmpty(dataRow[dataColumn.ColumnName].ToString()))
                    {
                        value = "";
                    }
                    else
                    {
                        value = System.Convert.ToDateTime(dataRow[dataColumn.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm");
                    }
                    dictionary.Add(dataColumn.ColumnName, value);
                }
                else
                {
                    dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
                }
            }
            list.Add(dictionary);
        }
        return this.Jss.Serialize(list);
    }

    public DataTable JsonToTable(string JsonString)
    {
        System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
        list = this.Jss.Deserialize<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>(JsonString);
        DataTable dataTable = new DataTable();
        if (list.Count > 0)
        {
            foreach (System.Collections.Generic.Dictionary<string, object> current in list)
            {
                if (dataTable.Columns.Count == 0)
                {
                    foreach (string current2 in current.Keys)
                    {
                        dataTable.Columns.Add(current2, current[current2].GetType());
                    }
                }
                DataRow dataRow = dataTable.NewRow();
                foreach (string current2 in current.Keys)
                {
                    dataRow[current2] = current[current2];
                }
                dataTable.Rows.Add(dataRow);
            }
        }
        return dataTable;
    }

    //public static string ToJson(object t)
    //{
    //    Formatting arg_12_1 = 1;
    //    JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
    //    jsonSerializerSettings.set_NullValueHandling(0);
    //    return JsonConvert.SerializeObject(t, arg_12_1, jsonSerializerSettings);
    //}

    //public static string ToJson(object t, bool HasNullIgnore)
    //{
    //    string result;
    //    if (HasNullIgnore)
    //    {
    //        Formatting arg_1A_1 = 1;
    //        JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
    //        jsonSerializerSettings.set_NullValueHandling(1);
    //        result = JsonConvert.SerializeObject(t, arg_1A_1, jsonSerializerSettings);
    //    }
    //    else
    //    {
    //        result = JsonOperate.ToJson(t);
    //    }
    //    return result;
    //}

    public static T FromJson<T>(string strJson) where T : class
    {
        T result;
        if (!string.IsNullOrEmpty(strJson))
        {
            result = JsonConvert.DeserializeObject<T>(strJson);
        }
        else
        {
            result = default(T);
        }
        return result;
    }
}
时间: 2024-10-25 06:08:52

JsonOperate 帮助类的相关文章

jvm系列(一):java类的加载机制

java类的加载机制 原文:http://www.cnblogs.com/ityouknow/p/5603287.html 1.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构.类的加载的最终产品是位于堆区中的Class对象,Class对象封装了类在方法区内的数据结构,并且向Java程序员提供了访问方法区内的数据结构的接口. 类加载器并不需要等到某个

iOS -- SKSpriteNode类

SKSpriteNode类 继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKSpriteNode.h 参考指南 Sprite Kit Progamming Guide 概览 重要提示:这是一个初步的API或者开发技术

iOS -- SKScene类

SKScene类 继承自 SKEffectNode:SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKScene.h 参考指南 Sprite Kit Progamming Guide 概览 重要提示:这是一个初步的API或者开

iOS -- SKPhysicsWorld类

SKPhysicsWorld类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKPhysicsWorld.h 参考指南 Sprite Kit Progamming Guide 概览 重要提示:这是一个初步的API或者开发技术文档.虽然已经审阅了本文档的技术准确性,但是它不是最终的版本.本机密信息仅适用于

C#嵌套类

嵌套类顾名思义就是类或者结构中定义的类 class Container { class Nested { Nested() { } } } <1>嵌套类的默认访问权限是private ,可以指定为public,protected,private,internal,protected internal.<2>嵌套类型可以访问外部类(包裹嵌套类的类),如果要访问外部类型,要把外部类通过构造函数传进一个实例<3>嵌套类中只能访问外部类中的静态成员,不能直接访问外部类的非静态成

一个实用的C#网页抓取类代码分享

一个实用的C# 网页抓取类 模拟蜘蛛,类中定义了超多的C#采集文章.网页抓取文章的基础技巧,下面分享代码: using System; using System.Data; using System.Configuration; using System.Net; using System.IO; using System.Text; using System.Collections.Generic; using System.Text.RegularExpressions; using Sys

类图(Rose) - Windows XP经典软件系列

最近开始了自己高级数据结构之旅,在这次旅行中,我将持续把一些高级的数据结构从理论到编码都过一遍,同时通过博客形式分享出来,希望大家指出不足之处! 二叉排序树是一种动态排序的数据结构,支持插入.删除.查找等操作,且平均时间复杂度为O(log(N)),但是普通二叉排序树不能保证树退化为一颗分支的情况,此时最坏情况下的时间复杂度为O(N).此时,平衡二叉树的产生了.平衡二叉树是一种动态调整平衡的数据结构,但理想的平衡二叉树很难,于是人们使用AVL.红黑树.Treap.伸展树等来替代平衡二叉树,这些数据

java 类对象使用

在学习反射机制时,总结一下获得类对象方式: 第一种方式:通过类本身来获得对象 Class<?> classname = this.getClass(); 或者this.class 第二种方式:通过子类的实例获取父类对象 ClassName cn = new ClassName(); UserClass = cn.getClass(); Class<?> SubUserClass = UserClass.getSuperclass(); 第三种方式:通过类名加.class获取对象 C

Python-class类的相关总结

在Python中,所有的数据类型都可以视为对象,自定义的对象数据类型就是面向对象中的类(class)的概念. 面向对象编程:object oriented programming简称OOP. 1 ###简单举例,以登记学生的姓名和成绩举例 2 #!/usr/bin/python 3 #-*- coding:utf-8 -*- 4 class Student(object): ##定义student类 5 def __init__(self, name, score): ##__init__可以绑