多层次的Json字符串转化为对象

using Arvato.CRM.DataTrans.ConsoleHost.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace Arvato.CRM.DataTrans.ConsoleHost.Helper
{
    public static class JsonHelper
    {
        #region Json字符串转换成对象


        /// <summary>
        /// 根据公司需求实现的Json反序列化方法
        /// </summary>
        /// <typeparam name="T">泛型实例</typeparam>
        /// <param name="JsonStr">Json串</param>
        /// <returns>MainJson<T>对象</returns>
        public static MainJson<T> JSONToList<T>(this string JsonStr)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            Serializer.MaxJsonLength = 52428800;//默认接受最大的长度是 2097152/1024/1024=2MB
            MainJson<T> objs = Serializer.Deserialize<MainJson<T>>(JsonStr);
            return objs;
        }

        public static List<T> JSONStringToList<T>(this string JsonStr)
        {
            JavaScriptSerializer Serializer = new JavaScriptSerializer();
            Serializer.MaxJsonLength = 52428800;
            List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
            return objs;
        }

        public static T Deserialize<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
                //System.ServiceModel.Web 和 System.Runtime.Serialization
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                return (T)serializer.ReadObject(ms);
            }
        }

        #endregion
    }
}

 


 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arvato.CRM.DataTrans.ConsoleHost.Model
{
    /// <summary>
    /// Json接口
    /// </summary>
    public class MainJson<T>
    {
        /// <summary>
        /// 1:成功 0:失败
        /// </summary>
        public int success { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string message { get; set; }

        /// <summary>
        /// 数据主体
        /// </summary>
        public body<T> body { get; set; }
    }

    /// <summary>
    /// 数据主体
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class body<T>
    {
        /// <summary>
        /// 实体
        /// </summary>
        public List<T> item { get; set; }

        /// <summary>
        /// 表示总记录数
        /// </summary>
        public int total { get; set; }
    }
}

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arvato.CRM.DataTrans.ConsoleHost.Model
{
    /// <summary>
    /// 城市列表
    /// </summary>
    public class CITY
    {
        /// <summary>
        /// 城市编码
        /// </summary>
        public int? cityid { get; set; }

        /// <summary>
        /// 城市名称
        /// </summary>
        public object name { get; set; }

        /// <summary>
        /// 最后更新时间戳
        /// </summary>
        public DateTime? utime { get; set; }
    }
}

 

 

 

调用时:

 


MainJson<CITY> city_List = JsonHelper.JSONToList<CITY>(JsonStr);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。