Json数据转换

using System.Web.Script.Serialization;//反序列化
using System.Collections;
using System.Runtime.Serialization.Json;//重要
using Newtonsoft.Json;//重要
using Newtonsoft.Json.Converters;//重要
using System.IO;
using System.Collections.Generic;

引入以上命名空间

        /// <summary>
        /// JSON反序列化
        /// </summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(ms);
            return obj;
        }

        public class checkinfo
        {
            public string code { get; set; }
            public string checkContent { get; set; }
            public string checkResult { get; set; }
            public string remark { get; set; }
        }

        public class checkinfolist
        {
            public List<checkinfo> list { get; set; }
        }



//使用方法
checkinfolist checkinfomodel = JsonDeserialize<checkinfolist>(info);




                      //序列化为json数据
             string sysIds = "";//主键id string txtDevicename = ""; string txtDevicemodel = ""; string txtcheckPosition = ""; if (context.Request.QueryString["sysIds"] != null) { sysIds = context.Request.QueryString["sysIds"]; } if (context.Request.QueryString["txtDevicename"] != null) { txtDevicename = context.Request.QueryString["txtDevicename"]; } if (context.Request.QueryString["txtDevicemodel"] != null) { txtDevicemodel = context.Request.QueryString["txtDevicemodel"]; } if (context.Request.QueryString["txtcheckPosition"] != null) { txtcheckPosition = context.Request.QueryString["txtcheckPosition"]; } DataSet emps5 = emp2.GetDeviceCheckList(sysIds, txtDevicename, txtDevicemodel, txtcheckPosition, intPageSize, intCurrentPage); ReturnValue = "{"total":" + emps5.Tables[1].Rows[0][0].ToString() + ", "rows":"; ReturnValue += JsonConvert.SerializeObject(emps5.Tables[0], new DataTableConverter()); ReturnValue += "}";

还有一种方法:

using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace ConsoleApplication1
{
    class Class1
    {
        static void Main(string[] args)
        {

            dynamic c;


            string inputJsonString = @" 
            {
               pa:[
                      {StudentID:'100',Name:'aaa',Hometown:'china'},
                      {StudentID:'101',Name:'bbb',Hometown:'us'},
                      {StudentID:'102',Name:'ccc',Hometown:'england'}
                  ]
             }";
             c = (object)JsonConvert.DeserializeObject(inputJsonString);
             string diyge = c.pa[0].StudentID;


dynamic d;


StringBuilder sdd=new StringBuilder();
string jsons = sdd.Append("{"SysId": "6a4a8876e718472388b5ec702fa27d39","StaffSysId": "863","list": [{"SureDay": "2016-02-26","DakaBegin": "","DakaEnd": "","Daystate": "6","WorkHours": "0","Remark": ""}]}").ToString();

d = (object)JsonConvert.DeserializeObject(jsons);
string di = d["list"][0]["Daystate"];//不同的取值方式



             Console.WriteLine(diyge);
            Console.ReadKey();
        }
    }
}
原文地址:https://www.cnblogs.com/Zpyboke/p/5209604.html