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

时间:2024-01-11 20:51:26

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
    }
}

如下是一个三层次的Json字符串的转换对象实例:

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; }
    }

/// <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);

再如2个层次的Json字符串转换对象时:

namespace CrazyAuchan.Common
{
    public class MainJson<T>
    {
        public int total { get; set; }
        public string message { get; set; }
        public int pageNo { get; set; }
        public int pageSize { get; set; }
        public int status { get; set; }
        ////0:新注册获得 1:消费获得 2:分享成功获得
        public int source { get; set; }
        public List<T> data { get; set; }
    }

public class CouponInfo
    {
        public string activityNo { get; set; }
        public string couponNo { get; set; }
        //Failure(0L, "已失效"), NoFailure(1L, "未失效")
        //0:已失效 1:未失效
        public int overdueStatus { get; set; }
        //HasShare(0L, "已分享"), DoesNotShare(1L, "未分享");
        //0:已分享 1:未分享
        public int shareStatus { get; set; }
        ////ANewRegistrationToObtain(0L, "新注册获得"), ConsumerToObtain(1L, "消费获得"), ShareSuccess(2L, "分享成功获得");
        ////0:新注册获得 1:消费获得 2:分享成功获得
        public int source { get; set; }
        //couponNo的HTML二维码
        public string couponCode { get; set; }
        //总条数
        public int itemCount { get; set; }
    }
}

调用时的代码:

var CouponList1 = new List<CouponInfo>();

var

CouponJson = "{\"data\":

[

{\"activityNo\":\"1111\",\"couponNo\":\"7200002000095\",\"overdueStatus\":1,\"shareStatus\":0},{\"activityNo\":\"1111\",\"couponNo\":\"7200000200094\",\"overdueStatus\":1,\"shareStatus\":1},

{\"activityNo\":\"1111\",\"couponNo\":\"7200002000093\",\"overdueStatus\":0,\"shareStatus\":0},{\"activityNo\":\"1111\",\"couponNo\":\"7200000200092\",\"overdueStatus\":1,\"shareStatus\":0},

{\"activityNo\":\"1111\",\"couponNo\":\"7200000200091\",\"overdueStatus\":1,\"shareStatus\":0},{\"activityNo\":\"1111\",\"couponNo\":\"7200000200090\",\"overdueStatus\":1,\"shareStatus\":1}

],

\"total\":16,

\"message\":\"符合申领资格!\",

\"pageNo\":1,

\"pageSize\":6,

\"status\":1,

\"source\":1

}";

if (!string.IsNullOrEmpty(CouponJson))
            {
                MainJson<CouponInfo> MainCouponInfo = JsonHelper.JSONToList<CouponInfo>(CouponJson);
                if (MainCouponInfo.status == 1)
                {
                    itemCount = MainCouponInfo.total;
                    CouponList1 = MainCouponInfo.data.Where(p => !string.IsNullOrEmpty(p.couponNo) && p.couponNo.Length == 13).ToList();
                }
            }

foreach (var coupon in CouponList1)
            {
                if (coupon != null && !string.IsNullOrEmpty(coupon.couponNo))
                {
                    coupon.itemCount = itemCount;
                    coupon.couponCode = BarCodeHelper.getBarCode13(coupon.couponNo, 2, 40);//"7200000000090"
                }
            }
            var json = JsonHelper.JsonSerialize(CouponList1);