最近在项目中用到的相关Json的数据转换
List<T>转Json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
static
string
Obj2Json<T>(T data)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
new
System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
using
(MemoryStream ms =
new
MemoryStream())
{
serializer.WriteObject(ms, data);
return
Encoding.UTF8.GetString(ms.ToArray());
}
}
catch
{
return
null
;
}
}
|
Json转List<T>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
static
Object Json2Obj(String json,Type t)
{
try
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
new
System.Runtime.Serialization.Json.DataContractJsonSerializer(t);
using
(MemoryStream ms =
new
MemoryStream(Encoding.UTF8.GetBytes(json)))
{
return
serializer.ReadObject(ms);
}
}
catch
{
return
null
;
}
}
|
DataTable 转Json
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
|
public
static
string
DataTable2Json(DataTable dt)
{
if
(dt.Rows.Count == 0)
{
return
""
;
}
StringBuilder jsonBuilder =
new
StringBuilder();
// jsonBuilder.Append("{");
//jsonBuilder.Append(dt.TableName.ToString());
jsonBuilder.Append(
"["
);
//转换成多个model的形式
for
(
int
i = 0; i < dt.Rows.Count; i++)
{
jsonBuilder.Append(
"{"
);
for
(
int
j = 0; j < dt.Columns.Count; j++)
{
jsonBuilder.Append(
"\""
);
jsonBuilder.Append(dt.Columns[j].ColumnName);
jsonBuilder.Append(
"\":\""
);
jsonBuilder.Append(dt.Rows[i][j].ToString());
jsonBuilder.Append(
"\","
);
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append(
"},"
);
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append(
"]"
);
// jsonBuilder.Append("}");
return
jsonBuilder.ToString();
}
|
单个对象转JSON
1
2
3
4
5
6
7
8
9
|
public
static
T Json2Obj<T>(
string
json)
{
T obj = Activator.CreateInstance<T>();
using
(System.IO.MemoryStream ms =
new
System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
{
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
new
System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
return
(T)serializer.ReadObject(ms);
}
}
|
转自:C#中List〈string〉和string[]数组之间的相互转换
1,从System.String[]转到List<System.String>
System.String[] str={"str","string","abc"};
List<System.String> listS=new List<System.String>(str);
2, 从List<System.String>转到System.String[]
List<System.String> listS=new List<System.String>();
listS.Add("str");
listS.Add("hello");
System.String[] str=listS.ToArray();
1、使用NewtonSoft.Json.dll
序列化:
string jsonStr= JavaScriptConvert.SerializeObject(person);//person为实例化之后的对象
反序列化:
Person person = (Person)JavaScriptConvert.DeserializeObject(jsonStr, typeof(Person));
2、使用JavaScriptSerializr进行Json的序列化和反序列化
序列化:
JavaScriptSerializer jss = new JavaScriptSerializer();
string jsonStr= jss.Serialize(person);
反序列化:Person p1=jss.Deserialize<Person>(jsonStr);
序列化list对象:
public string Serialize( JavaScriptSerializer serializer) { List list = new PersonMgr().getPersons(); JsonArrayCollection jac = new JsonArrayCollection(); foreach (Person p in list) { JsonObjectCollection joc = new JsonObjectCollection(); Dictionary row = new Dictionary(); foreach (System.Reflection.PropertyInfo pro in p.GetType().GetProperties()) { joc.Add(new JsonStringValue(pro.Name,pro.GetValue(p,null).ToString())); } jac.Add(joc); } return jac.ToString(); }
反序列化:
public List<object> DeSerialize<T>(string jsonStr) { List<object> list = new List<object>(); JsonTextParser jtp = new JsonTextParser(); JsonArrayCollection jac = jtp.Parse(jsonStr) as JsonArrayCollection; T o = Activator.CreateInstance<T>(); foreach (JsonObjectCollection joc in jac) { using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(joc.ToString()))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(o.GetType()); list.Add((T)serializer.ReadObject(ms)); } } return list; }
一个人的牛逼不如一群人傻逼