JSONHelper

来源:https://www.cnblogs.com/zclaude/archive/2018/12/06/10074822.html
-Advertisement-
Play Games

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.IO;using System.Text;using System.Web.Script. ...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;



[Serializable] public static class JSONHelper { /// <summary> /// 對象轉JSON /// </summary> /// <param name="obj">對象</param> /// <returns>JSON格式的字元串</returns> public static string ObjectToJSON(object obj) { JavaScriptSerializer jss = new JavaScriptSerializer(); try { return jss.Serialize(obj); } catch { } return null; } /// <summary> /// /// JSON文本轉對象,泛型方法 /// </summary> /// <typeparam name="T">類型</typeparam> /// <param name="jsonText">JSON文本</param> /// <returns>指定類型的對象</returns> public static T JSONToObject<T>(string jsonText) { JavaScriptSerializer jss = new JavaScriptSerializer(); try { return jss.Deserialize<T>(jsonText); } catch{} return default(T); } public static string ObjectToJsonDateTime(object obj) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DateTimeConverter() }); try { return jss.Serialize(obj); } catch { } return null; } }


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
    


public class DateTimeConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            return new JavaScriptSerializer().ConvertToType(dictionary, type);
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            if (!(obj is DateTime))
            {
                return null;
            }
            return new CustomString(((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss"));
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new[] { typeof(DateTime) };
            }
        }

        private class CustomString : Uri, IDictionary<string, object>
        {
            public CustomString(string str) : base(str, UriKind.Relative)
            {
            }

            void IDictionary<string, object>.Add(string key, object value)
            {
                throw new NotImplementedException();
            }

            bool IDictionary<string, object>.ContainsKey(string key)
            {
                throw new NotImplementedException();
            }

            ICollection<string> IDictionary<string, object>.Keys
            {
                get
                {
                    throw new NotImplementedException();
                }

            }

            bool IDictionary<string, object>.Remove(string key)
            {
                throw new NotImplementedException();
            }


            bool IDictionary<string, object>.TryGetValue(string key, out object value)
            {
                throw new NotImplementedException();

            }

            ICollection<object> IDictionary<string, object>.Values
            {

                get
                {
                    throw new NotImplementedException();
                }

            }

            object IDictionary<string, object>.this[string key]
            {
                get
                {
                    throw new NotImplementedException();
                }
                set
                {
                    throw new NotImplementedException();
                }
            }

            void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
            {
                throw new NotImplementedException();
            }

            void ICollection<KeyValuePair<string, object>>.Clear()
            {
                throw new NotImplementedException();
            }

            bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
            {

                throw new NotImplementedException();

            }

            void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
            {
                throw new NotImplementedException();

            }

            int ICollection<KeyValuePair<string, object>>.Count
            {
                get
                {
                    throw new NotImplementedException();
                }
            }

            bool ICollection<KeyValuePair<string, object>>.IsReadOnly
            {
                get
                {
                    throw new NotImplementedException();
                }
            }

            bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
            {
                throw new NotImplementedException();
            }

            IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
            {
                throw new NotImplementedException();
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                throw new NotImplementedException();
            }
        }
    }

  

  


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文主要介紹了關於PHP如何實現我們大家都知道的開心消消樂的演算法,分享PHP教程出來供大家參考學習,下麵話不多說了,來一起看看詳細的介紹吧。 一、需求描述: 1、在一個8*8的矩陣方格中隨機出現5種顏色的色塊。 2、當有三個或以上色塊在橫向或縱向上相連,則消除這些色塊。 3、色塊消除後,上方色塊往下 ...
  • bin()十進位轉二進位; oct()十進位轉二進位; hex()十進位轉二進位; 字元串: 索引: 查找: 移除空白: 長度: 替換: 切片: 分割: 連接: 首字母大寫; 居中、對齊: 計數: 以什麼開頭、結尾: 大小寫: 格式化輸出: ...
  • 1 開發環境: 1、eclipse(可替換) 2、jfreechart-1.0.19 2 說明: (1) source目錄:為 jfreechart的源碼目錄;不會的主要看這裡。因為他的文檔是收費的。 (2) lib目錄:為包目錄,我們需要關註的包為 jfreechart-1.0.10.jar、 g ...
  • 引言 小A正在balabala寫代碼呢,DBA小B突然發來了一條消息,“快看看你的用戶特定信息表T,裡面的主鍵,也就是自增id,都到16億了,這才多久,在這樣下去過不了多久主鍵就要超出範圍了,插入就會失敗,balabala......” 我記得沒有這麼多,最多1k多萬,count了下,果然是1100 ...
  • 題意 "題目鏈接" Sol NOI的題都這麼良心麽。。 先交個$n^4$暴力 = 75 hash優化一下 = 90 然後$90$到$100$分之間至少差了$10$難度臺階= = $90$分的暴力hash就比較trival了。 考慮怎麼優化。 顯然我們只要找出所有形如$AA$的字元串就行了,設$pre ...
  • 簡介: 正則表達式本身是一個小型的、高度專業化的編程語言,而在python中,通過內嵌集成re模塊,我們可以通過直接調用來實現正則匹配。 正則表達式基礎知識: --普通字元匹配自身 abc abc --元字元 . :匹配任何字元(\n)換行符除外例:a.a aba \ :轉義字元例:a\tb a b ...
  • iOS Swift WisdomHUD 提示界面框架 Framework Use profile(應用簡介) 一:WisdomHUD簡介 今天給大家介紹一款iOS的界面顯示器:WisdomHUD,WisdomHUD是Swift 4.2編寫的framework,支持iOS8.0及以上使用,並且完成相容 ...
  • 1. 構造器實例化 spring容器通過bean對應的預設的構造函數來實例化bean。 2. 靜態工廠方式實例化 首先創建一個靜態工廠類,在類中定義一個靜態方法創建實例。 靜態工廠類及靜態方法: xml配置文件 3. 實例工廠方式實例化 該種方式的工廠類中,不再使用靜態方法創建Bean實例,而是採用 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...