zoukankan      html  css  js  c++  java
  • 高效Json类 操作数据

    废话不多说 上代码 :

     也是建立与    Newtonsoft.Json 的基础上,增加对datatime类型的处理,比较方便直观

     

    用法:

    json.Encode  

    json.Decode

     

     

    代码见下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Text;
      4 using System.Collections;
      5 
      6 using Newtonsoft.Json;
      7 using Newtonsoft.Json.Linq;
      8 using Newtonsoft.Json.Converters;
      9 /// <summary>
     10 ///JSON 的摘要说明
     11 /// </summary>
     12 public class JSON
     13 {
     14 public static string DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
     15 public static string DateTimeFormat2 = "yyyy'-'MM'-'dd";
     16 public static string Encode(object o)
     17 {
     18 if (o == null || o.ToString() == "null") return null;
     19 
     20 if (o != null && (o.GetType() == typeof(String) || o.GetType() == typeof(string)))
     21 {
     22 return o.ToString();
     23 }
     24 IsoDateTimeConverter dt = new IsoDateTimeConverter();
     25 dt.DateTimeFormat = DateTimeFormat;
     26 return JsonConvert.SerializeObject(o, dt);
     27 }
     28 public static JArray ConvertJArray(ArrayList list)
     29 {
     30 string json = Encode(list);
     31 return JArray.Parse(json);
     32 }
     33 public static JObject ConvertJObject(Hashtable ht)
     34 {
     35 string json = Encode(ht);
     36 return JObject.Parse(json);
     37 }
     38 public static string EncodeShortDate(object o)
     39 {
     40 if (o == null || o.ToString() == "null") return null;
     41 
     42 if (o != null && (o.GetType() == typeof(String) || o.GetType() == typeof(string)))
     43 {
     44 return o.ToString();
     45 }
     46 IsoDateTimeConverter dt = new IsoDateTimeConverter();
     47 dt.DateTimeFormat = DateTimeFormat2;
     48 return JsonConvert.SerializeObject(o, dt);
     49 }
     50 public static object Decode(string json)
     51 {
     52 if (String.IsNullOrEmpty(json)) return "";
     53 object o = JsonConvert.DeserializeObject(json);
     54 if (o.GetType() == typeof(String) || o.GetType() == typeof(string))
     55 {
     56 o = JsonConvert.DeserializeObject(o.ToString());
     57 }
     58 object v = toObject(o);
     59 return v;
     60 }
     61 public static object Decode(string json, Type type)
     62 {
     63 return JsonConvert.DeserializeObject(json, type);
     64 }
     65 private static object toObject(object o)
     66 {
     67 if (o == null) return null;
     68 
     69 if (o.GetType() == typeof(string))
     70 {
     71 //判断是否符合2010-09-02T10:00:00的格式
     72 string s = o.ToString();
     73 if (s.Length == 19 && s[10] == 'T' && s[4] == '-' && s[13] == ':')
     74 {
     75 o = System.Convert.ToDateTime(o);
     76 }
     77 }
     78 else if (o is JObject)
     79 {
     80 JObject jo = o as JObject;
     81 
     82 Hashtable h = new Hashtable();
     83 
     84 foreach (KeyValuePair<string, JToken> entry in jo)
     85 {
     86 h[entry.Key] = toObject(entry.Value);
     87 }
     88 
     89 o = h;
     90 }
     91 else if (o is IList)
     92 {
     93 
     94 ArrayList list = new ArrayList();
     95 list.AddRange((o as IList));
     96 int i = 0, l = list.Count;
     97 for (; i < l; i++)
     98 {
     99 list[i] = toObject(list[i]);
    100 }
    101 o = list;
    102 
    103 }
    104 else if (typeof(JValue) == o.GetType())
    105 {
    106 JValue v = (JValue)o;
    107 o = toObject(v.Value);
    108 }
    109 else
    110 {
    111 }
    112 return o;
    113 }
    114 }
  • 相关阅读:
    一起来学linux:网络命令
    python网络爬虫之使用scrapy自动登录网站
    python网络爬虫之requests库 二
    一起来学linux:网络配置
    一起来学linux:日志文件
    一起来学linux:进程
    python网络爬虫之requests库
    【原创】大数据基础之Ambari(1)简介、编译安装、使用
    【原创】数据库基础之Mysql(1)常用命令
    【原创】大叔经验分享(22)securecrt连接自动断开
  • 原文地址:https://www.cnblogs.com/zonzi/p/12674701.html
Copyright © 2011-2022 走看看