zoukankan      html  css  js  c++  java
  • Json数据序列化对象,及对象序列化为Json格式

    View Code
    public class JsonHelper
    {
    /// <summary>
    /// 生成Json格式
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="obj"></param>
    /// <returns></returns>
    public static string GetJson<T>(T obj)
    {
    DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());
    using (MemoryStream stream = new MemoryStream())
    {
    json.WriteObject(stream, obj);
    string szJson = Encoding.UTF8.GetString(stream.ToArray());
    return szJson;
    }
    }
    /// <summary>
    /// 转换Json的Model对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="szJson"></param>
    /// <returns></returns>
    public static T ParseFromJson<T>(string szJson)
    {
    T obj = Activator.CreateInstance<T>();
    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
    {
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
    return (T)serializer.ReadObject(ms);
    }
    }
    /// <summary>
    /// 根据Json获取Dictionary集合
    /// </summary>
    /// <param name="szJson"></param>
    /// <returns></returns>
    public static Dictionary<string, string> GetDictionaryFromJson(string szJson)
    {
    Dictionary<string, string> list = new Dictionary<string, string>();

    System.Web.Script.Serialization.JavaScriptSerializer jsonConvert = new System.Web.Script.Serialization.JavaScriptSerializer();
    list = jsonConvert.Deserialize<Dictionary<string, string>>(szJson);

    return list;
    }
    }
  • 相关阅读:
    Tableau如何绘制堆叠柱状图
    Tableau如何绘制多边形地图
    Tableau如何绘制凹凸图
    数据库备份策略设计
    MySQL数据库如何实现增量备份
    Tableau如何绘制瀑布图
    创建.net core ef项目
    asp.net core的ef的连接字符串
    sqlserver的varchar nvarchar区别
    jq的ajax请求写法
  • 原文地址:https://www.cnblogs.com/snlfq2000/p/2337209.html
Copyright © 2011-2022 走看看