zoukankan      html  css  js  c++  java
  • JSON反序列化


    /// <summary>

    /// JSON反序列化

    /// </summary>

    public static T JsonDeserialize<T>(string jsonString)
    {

    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));

    T obj = (T)ser.ReadObject(ms);

    return obj;

    }

    // List<shopMode> obj = util.HttpWeb.DeserializeFromJson<List<shopMode>>(r);
    /// <summary>
    /// JSON反序列化集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="jsonString"></param>
    /// <returns></returns>
    public static T DeserializeFromJson<T>(string jsonString) where T : class
    {
    DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
    {
    T obj = (T)ds.ReadObject(ms);
    return obj;
    }
    }

    var json = "[{"protype_id":14,"bellType":0,"tech_number":7722},{"protype_id":14,"bellType":0,"tech_number":4272},{"protype_id":12,"bellType":0,"tech_number":3225},{"protype_id":12,"bellType":0,"tech_number":3208},{"protype_id":11,"bellType":0,"tech_number":8061},{"protype_id":11,"bellType":0,"tech_number":3216},{"protype_id":11,"bellType":0,"tech_number":6144},{"protype_id":11,"bellType":0,"tech_number":6132},{"protype_id":11,"bellType":0,"tech_number":6272}]";

    JavaScriptSerializer js = new JavaScriptSerializer();
    List<techItem> team = js.Deserialize<List<techItem>>(json);

    //JsonConvert静态方式解析数据
    var result = JsonConvert.DeserializeObject<T>(json);
    var list = JsonConvert.DeserializeObject<List<T>>(json);

  • 相关阅读:
    SqlServer怎样获取查询语句的成本
    Testcase中Debug 提示
    cmd batch use variable
    主流数据库默认端口
    Usage of doskey
    操作系统shell的比较/Comparison of command shells
    延长windows激活时间
    一道面试题和一个结果.
    注册表操作CMD(reg.exe)
    adb 查看固件版本
  • 原文地址:https://www.cnblogs.com/sxmny/p/4137899.html
Copyright © 2011-2022 走看看