json串中包含普通类型,list<>数组类型,自定义对象类型
一、实体类代码片段
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApi.Entity
{
public class Header
{
/// <summary>
/// 渠道id
/// </summary>
public string appId { get; set; }
/// <summary>
/// 版本号
/// </summary>
public string version { get; set; }
/// <summary>
/// 当前时间
/// </summary>
public string timestamp { get; set; }
/// <summary>
/// 加密方式
/// </summary>
public string encType { get; set; }
/// <summary>
/// 加密数据
/// </summary>
public string encData { get; set; }
/// <summary>
/// 签名方式
/// </summary>
public string signType { get; set; }
/// <summary>
/// 签名串
/// </summary>
public string signData { get; set; }
/// <summary>
/// 交易编号
/// </summary>
public string infno { get; set; }
public Connect connect { get; set; }
public List<string> c { get; set; }
}
public class Connect{
public string a { get; set; }
public string b { get; set; }
}
}
二、序列化和反序列化帮助类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace WebApi.Common
{
public class JsonHelper
{
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string Serialize<T>(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, obj);
string retVal = Encoding.UTF8.GetString(ms.ToArray());
return retVal;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
ms.Close();
ms.Dispose();
return obj;
}
}
}
三、代码实现json序列化以及反序列化
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Common;
using WebApi.Entity;
namespace WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class apiserveController : ControllerBase
{
[HttpGet("Serialize")]
public string Cs() {
Header header = new Header();
Connect connect = new Connect();
List<string> c = new List<string>();
c.Add("1");
c.Add("2");
c.Add("3");
string a = "";
header.appId = "1111";
header.version = "1.0";
header.timestamp = "a";
header.encType = "SM2";
header.encData = "222";
header.signType = "SM4";
header.signData = "asda";
header.infno = "1";
header.c = c;
connect.a = "a";
connect.b = "b";
header.connect = connect;
a = JsonHelper.Serialize<Header>(header);
return a;
}
[HttpGet("Deserialize")]
public List<Header> Toentity(string a) {
Header c = JsonHelper.Deserialize<Header>(a);
List<Header> b = new List<Header>();
b.Add(c);
return b;
}
}
}