songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Data;
using System.Web.Script.Serialization;
 
namespace BatchService.Framework.Utility
{
    public class JsonHelper
    {
        public static string GetValue(string i_data, string i_key)
        {
            JObject t_obj = JObject.Parse(i_data);
            if (t_obj == null || t_obj[i_key] == null)
            {
                return "";
            }
            string t_ret = t_obj[i_key].ToString();
 
            return t_ret;
        }
 
        public static string GetJsonFromObject<T>(T obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
 
        /// <summary>   
        /// Json序列化   
        /// </summary>   
        public static string JsonSerializer<T>(T obj)
        {
            string jsonString = string.Empty;
            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
 
                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, obj);
                    jsonString = Encoding.UTF8.GetString(ms.ToArray());
                }
            }
            catch(Exception ex)
            {
                jsonString = string.Empty;
                throw ex;
            }
            return jsonString;
        }
 
        /// <summary>   
        /// Json反序列化  
        /// </summary>   
        public static T JsonDeserialize<T>(string jsonString)
        {
            T obj = Activator.CreateInstance<T>();
            try
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());//typeof(T)  
                    T jsonObject = (T)ser.ReadObject(ms);
                    ms.Close();
 
                    return jsonObject;
                }
            }
            catch(Exception ex)
            {
                LogHelper.Error(ex.ToString());
                return default(T);
            }
        }
 
        // 将 DataTable 序列化成 json 字符串  
        public static string DataTableToJson(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return "\"\"";
            }
            JavaScriptSerializer myJson = new JavaScriptSerializer();
 
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
 
            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> result = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    result.Add(dc.ColumnName, dr[dc].ToString());
                }
                list.Add(result);
            }
            return myJson.Serialize(list);
        }
 
        // 将对象序列化成 json 字符串  
        public static string ObjectToJson(object obj)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            JavaScriptSerializer myJson = new JavaScriptSerializer();
 
            return myJson.Serialize(obj);
        }
 
        // 将 json 字符串反序列化成对象  
        public static object JsonToObject(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return null;
            }
            JavaScriptSerializer myJson = new JavaScriptSerializer();
 
            return myJson.DeserializeObject(json);
        }
 
        // 将 json 字符串反序列化成对象  
        public static T JsonToObject<T>(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return default(T);
            }
            JavaScriptSerializer myJson = new JavaScriptSerializer();
 
            return myJson.Deserialize<T>(json);
        }  
    }
}