zoukankan      html  css  js  c++  java
  • C# Json字符串反序列化

    using DevComponents.DotNetBar;
    using MyControl;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web.Script.Serialization;
    using System.Windows.Forms;
    
    namespace InternetDis
    {
        public class clsSystem
        {
            public static T JsonStringToCls<T>(string JsonStr)
            {
                StringReader strReader = new StringReader(JsonStr);
                T clsObj = (T)(new JsonSerializer()).Deserialize(new JsonTextReader(strReader), typeof(T));
                return clsObj;
            }
        }

      public class JsonResult { public string result { get; set; } public string description { get; set; } } }

    调用方法:

    JsonResult JsonRst = new JsonResult() { result = "-1" };
    StreamReader reader = new StreamReader(stream);
    string strRst = reader.ReadToEnd();
    JsonRst = clsSystem.JsonStringToCls<JsonResult>(strRst);

    需要添加 Newtonsoft.Json.dll 引用!

  • 相关阅读:
    MSMQ简例
    C#观察者模式简例
    C#常见算法题目(面试准备)
    HttpWebRequest
    自定义Attribute简例
    .Net下的 ORM框架介紹
    for xml path的应用
    .net中日至框架log4net.dll如何使用
    动态载入.ascx用户控件
    wap 2.0 编写规范
  • 原文地址:https://www.cnblogs.com/SuperMetalMax/p/6187313.html
Copyright © 2011-2022 走看看