c# 解析json数据

1 解析需要用到 Newtonsoft.Json.dll ,c#需要引用此dll

2 添加一个类文件JSON.cs类文件,代码如下

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
/*这里就不要动了,要添加就到部分类中去添加*/
namespace WindowsFormsApplication7
{
    public static partial class JSON
    {


        /// 
        /// 使用Newtonsoft.Json进行对象转换字符串
        /// 
        /// 对象
        /// json字符串
        public static string SerializeObj(object jsonObject)
        {
            return JsonConvert.SerializeObject(jsonObject);

        }
        /// 
        /// 使用Newtonsoft.Json进行字符串转换对象
        /// 
        /// 
        /// 
        /// 
        public static T DeSerializeObj(string json)
        {
            return JsonConvert.DeserializeObject(json);
        }
        /// 
        /// 使用Newtonsoft.Json进行字符串转换JArray数组
        /// 
        /// 
        /// 
        public static JArray SerializeObj(string json)
        {
            return JArray.Parse(json);

        }
        /// 
        /// datatable转json
        /// 
        /// 
        /// 
        public static string TableToJson(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString());
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder = jsonBuilder.Length == 0 ? jsonBuilder.Append("[") : jsonBuilder;
            jsonBuilder.Append("]");
            return jsonBuilder.ToString();
        }
        public static DataTable ToDataTable(this string json)
        {
            DataTable dataTable = new DataTable();  //实例化
            DataTable result;
            try
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                ArrayList arrayList = javaScriptSerializer.Deserialize(json);
                if (arrayList.Count > 0)
                {
                    foreach (Dictionary dictionary in arrayList)
                    {
                        if (dictionary.Keys.Count == 0)
                        {
                            result = dataTable;
                            return result;
                        }
                        if (dataTable.Columns.Count == 0)
                        {
                            foreach (string current in dictionary.Keys)
                            {
                                dataTable.Columns.Add(current, dictionary[current].GetType());
                            }
                        }
                        DataRow dataRow = dataTable.NewRow();
                        foreach (string current in dictionary.Keys)
                        {
                            dataRow[current] = dictionary[current];
                        }

                        dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                    }
                }
            }
            catch
            {
            }
            result = dataTable;
            return result;
        }
    }
}

3  定义解析json所使用的类

   public class test
        {
            private string _code;

            public string code
            {
                get { return _code; }
                set { _code = value; }
            }
            private string _message;

            public string message
            {
                get { return _message; }
                set { _message = value; }
            }
            private List _data;

            public List data
            {
                get { return _data; }
                set { _data = value; }
            }
        }
        public class Datas
        {
            private string _name1;

            public string name1
            {
                get { return _name1; }
                set { _name1 = value; }
            }
            private string _name2;

            public string name2
            {
                get { return _name2; }
                set { _name2 = value; }
            }
        }
4 解析json代码入下  第3点所声明的类 是对应的json字符串中的内容声明,请自行根据自己要解析的json声明类文件。

string json=  @"{""code"": ""S"",""message"": ""SUCCESS"", ""data"": [{""name1"": ""value1"",""name2"": ""value2""},{""name1"": ""value1"",""name2"": ""value2""}]}";
          var  obj = JSON.DeSerializeObj(json);
          obj.data[0].name1.ToString();


    


你可能感兴趣的:(c# 解析json数据)