zoukankan      html  css  js  c++  java
  • c# JsonHelper

    1.NuGet添加Newtonsoft.Json

    2.实例代码:

    public class JsonHelper
    {
    /// <summary>
    /// 解析JSON字符串生成对象实体
    /// </summary>
    /// <typeparam name="T">对象类型</typeparam>
    /// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param>
    /// <returns>对象实体</returns>
    public static T DeserializeJsonToObject<T>(string json) where T : class
    {
    JsonSerializer serializer = new JsonSerializer();
    StringReader sr = new StringReader(json);
    object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
    T t = o as T;
    return t;
    }


    /// <summary>
    /// 解析JSON数组生成对象实体集合
    /// </summary>
    /// <typeparam name="T">对象类型</typeparam>
    /// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
    /// <returns>对象实体集合</returns>
    public static List<T> DeserializeJsonToList<T>(string json) where T : class
    {
    JsonSerializer serializer = new JsonSerializer();
    StringReader sr = new StringReader(json);
    object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
    List<T> list = o as List<T>;
    return list;
    }
    }

  • 相关阅读:
    z-index优先级小结
    如何消除img间的默认间隙
    text-align和vertical-align
    HTTP
    HTTP
    HTTP
    HTTP
    ES6标准入门
    ES6标准入门
    ES6标准入门
  • 原文地址:https://www.cnblogs.com/xiewei123/p/10902613.html
Copyright © 2011-2022 走看看