zoukankan      html  css  js  c++  java
  • JSON格式转换

    代码1:

    引用:System.ServiceModel.Web程序集(系统内置)

    public static T JSONDecode<T>(string json)
    {
        JsonQueryStringConverter converter = new JsonQueryStringConverter();
        return (T) converter.ConvertStringToValue(json, typeof(T));
    }
    public static string JSONEncode<T>(T obj)
    {
        JsonQueryStringConverter converter = new JsonQueryStringConverter();
        return converter.ConvertValueToString(obj, typeof(T));
    }

    代码2:
    引用:System.Text.RegularExpressions; System.Runtime.Serialization.Json;

    public static string ObjectToJson<T>(T t)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    serializer.WriteObject(ms, t);
                    ms.Position = 0;
                    using (StreamReader reader = new StreamReader(ms))
                    {
                        string json = reader.ReadToEnd();
                        string p = @"\/Date((d+)+d+)\/";
                        MatchEvaluator evaluator = new MatchEvaluator(ConvertJsonDataToDataString);
                        Regex reg = new Regex(p);
                        json = reg.Replace(json, evaluator);
                        return json;
                    }
                }
    
            }
    
            public static T JsonToObject<T>(string json)
            {
                string p = @"d{4}-d{2}-d{2}sd{2}:d{2}:d{2}";
                MatchEvaluator evaluator = new MatchEvaluator(ConvertDateStringToJsonDate);    //对时间进行处理
                Regex reg = new Regex(p);
                json = reg.Replace(json, evaluator);
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    JsonQueryStringConverter json1 = new JsonQueryStringConverter();
                    return (T)json1.ConvertStringToValue(json, typeof(T));
                    T data = (T)serializer.ReadObject(ms);
                    return data;
                }
            }
  • 相关阅读:
    wxpython 简单例子:显示文本框的窗口显示鼠标位置
    wxpython学习:创建最小的空的wxPython程序
    wxPython学习笔记
    5G PDCCH 协议
    FPGA学习
    CCS 5.5下载地址http://www.dianyuan.com/bbs/1492792.html
    朴素贝叶斯
    决策树最后的存储 检测
    决策树 绘图
    决策树 书上的例题
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3544047.html
Copyright © 2011-2022 走看看