DataContractJsonSerializer 类 操作json类型数据

前端之家收集整理的这篇文章主要介绍了DataContractJsonSerializer 类 操作json类型数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

将对象序列化为 JavaScript 对象表示法 (JSON),并将 JSON 数据反序列化为对象。 此类不能继承。

//  msdn 例子:

namespace @H_404_17@SL_DataContractJsonSerializer
{
public @H_404_17@ partial @H_404_17@ class @H_404_17@Page:UserControl
{
public @H_404_17@Page()
{
InitializeComponent();
}

// Thisusesaneventhandler,notSLdatabinding
@H_404_17@ void @H_404_17@OnClick( object @H_404_17@sender,EventArgsargs)
{
txtOutput1.Text @H_404_17@= @H_404_17@ " CreateaUserobjectandserializeit. " @H_404_17@;
string @H_404_17@json @H_404_17@= @H_404_17@WriteFromObject();
txtOutput2.Text @H_404_17@= @H_404_17@json.ToString(); Displays:{"Age":42,"Name":"Bob"}
@H_404_17@
txtOutput3.Text @H_404_17@= @H_404_17@ DeserializethedatatoaUserobject. string @H_404_17@jsonString @H_404_17@= @H_404_17@ {'Name':'Bill','Age':53} " @H_404_17@;
UserdeserializedUser @H_404_17@= @H_404_17@ReadToObject(jsonString);
txtOutput4.Text @H_404_17@= @H_404_17@deserializedUser.Name; Displays:Bill
@H_404_17@txtOutput5.Text @H_404_17@= @H_404_17@deserializedUser.Age.ToString(); Displays:53
@H_404_17@}
CreateaUserobjectandserializeittoaJSONstream. static @H_404_17@ string @H_404_17@WriteFromObject()
{
CreateUserobject.
@H_404_17@Useruser @H_404_17@= @H_404_17@ new @H_404_17@User( Bob " @H_404_17@, 42 @H_404_17@);

Createastreamtoserializetheobjectto.
@H_404_17@MemoryStreamms @H_404_17@= @H_404_17@ new @H_404_17@MemoryStream();

SerializertheUserobjecttothestream.
@H_404_17@DataContractJsonSerializerser @H_404_17@= @H_404_17@ new @H_404_17@DataContractJsonSerializer( typeof @H_404_17@(User));
ser.WriteObject(ms,user);
byte @H_404_17@[]json @H_404_17@= @H_404_17@ms.ToArray();
ms.Close();
return @H_404_17@Encoding.UTF8.GetString(json,128); line-height:1.5!important">0 @H_404_17@,json.Length);

}

DeserializeaJSONstreamtoaUserobject. static @H_404_17@UserReadToObject( string @H_404_17@json)
{
UserdeserializedUser @H_404_17@= @H_404_17@ new @H_404_17@User();
MemoryStreamms @H_404_17@= @H_404_17@ new @H_404_17@MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializerser @H_404_17@= @H_404_17@ new @H_404_17@DataContractJsonSerializer(deserializedUser.GetType());
deserializedUser @H_404_17@= @H_404_17@ser.ReadObject(ms) as @H_404_17@User;
ms.Close();
return @H_404_17@deserializedUser;
}

}

[DataContract]
class @H_404_17@User
{
[DataMember]
string @H_404_17@Name{ get @H_404_17@; set @H_404_17@;}

[DataMember]
int @H_404_17@Age{ set @H_404_17@;}

public @H_404_17@User(){}

public @H_404_17@User( string @H_404_17@newName, int @H_404_17@newAge)
{
Name @H_404_17@= @H_404_17@newName;
Age @H_404_17@= @H_404_17@newAge;
}

}

}


可以抽象成如下类:

@H_404_17@ public @H_404_17@ class @H_404_17@JsonHelper
{
/// <summary>
生成Json格式
</summary> <typeparamname="T"></typeparam> <paramname="obj"></param> <returns></returns>
@H_404_17@ static @H_404_17@ string @H_404_17@GetJson @H_404_17@< @H_404_17@T @H_404_17@> @H_404_17@(Tobj)
{
DataContractJsonSerializerjson @H_404_17@= @H_404_17@ new @H_404_17@DataContractJsonSerializer(obj.GetType());
using @H_404_17@(MemoryStreamstream @H_404_17@= @H_404_17@ new @H_404_17@MemoryStream())
{
json.WriteObject(stream,obj);
string @H_404_17@szJson @H_404_17@= @H_404_17@Encoding.UTF8.GetString(stream.ToArray());
return @H_404_17@szJson;
}
}
获取Json的Model
<paramname="szJson"></param> static @H_404_17@TParseFromJson @H_404_17@< @H_404_17@T @H_404_17@> @H_404_17@( string @H_404_17@szJson)
{
Tobj @H_404_17@= @H_404_17@Activator.CreateInstance @H_404_17@< @H_404_17@T @H_404_17@> @H_404_17@();
using @H_404_17@(MemoryStreamms @H_404_17@= @H_404_17@ new @H_404_17@MemoryStream(Encoding.UTF8.GetBytes(szJson)))
{
DataContractJsonSerializerserializer @H_404_17@= @H_404_17@ return @H_404_17@(T)serializer.ReadObject(ms);
}
}
}
@H_404_17@ 反回JSON数据到前台
<paramname="dt"> 数据表 </param> <returns> JSON字符串 </returns> string @H_404_17@DataTableToJson(DataTabledt)
{
StringBuilderJsonString @H_404_17@= @H_404_17@ new @H_404_17@StringBuilder();
if @H_404_17@(dt @H_404_17@!= @H_404_17@ null @H_404_17@ @H_404_17@&& @H_404_17@dt.Rows.Count @H_404_17@> @H_404_17@ 0 @H_404_17@)
{
JsonString.Append( " { " @H_404_17@);
JsonString.Append( \ " @H_404_17@TableInfo\ :[ " @H_404_17@);
for @H_404_17@( int @H_404_17@i @H_404_17@= @H_404_17@ 0 @H_404_17@;i @H_404_17@< @H_404_17@dt.Rows.Count;i @H_404_17@++ @H_404_17@)
{
JsonString.Append( int @H_404_17@j @H_404_17@= @H_404_17@ 0 @H_404_17@;j @H_404_17@< @H_404_17@dt.Columns.Count;j @H_404_17@++ @H_404_17@)
{
if @H_404_17@(j @H_404_17@< @H_404_17@dt.Columns.Count @H_404_17@- @H_404_17@ 1 @H_404_17@)
{
JsonString.Append( "" +dt.Columns[j].ColumnName.ToString()+ " @H_404_17@\ : " @H_404_17@ @H_404_17@+ @H_404_17@ +dt.Rows[i][j].ToString()+ , " @H_404_17@);
}
else @H_404_17@ if @H_404_17@(j @H_404_17@== @H_404_17@dt.Columns.Count @H_404_17@- @H_404_17@ "" @H_404_17@);
}
}
if @H_404_17@(i @H_404_17@== @H_404_17@dt.Rows.Count @H_404_17@- @H_404_17@ } else @H_404_17@
{
JsonString.Append( },0); line-height:1.5!important">" @H_404_17@);
}
}
JsonString.Append( ]} return @H_404_17@JsonString.ToString();
}
else @H_404_17@
{
return @H_404_17@ null @H_404_17@;
}
}

//还有一种方式操作json类型数据:

class @H_404_17@JsonTableHelper
{
返回对象序列化
<paramname="obj"> 源对象 json数据 </returns> string @H_404_17@ToJson( this @H_404_17@ object @H_404_17@obj)
{
JavaScriptSerializerserialize @H_404_17@= @H_404_17@ new @H_404_17@JavaScriptSerializer();
return @H_404_17@serialize.Serialize(obj);
}

控制深度
<paramname="recursionDepth"> 深度 object @H_404_17@obj, int @H_404_17@recursionDepth)
{
JavaScriptSerializerserialize @H_404_17@= @H_404_17@ new @H_404_17@JavaScriptSerializer();
serialize.RecursionLimit @H_404_17@= @H_404_17@recursionDepth;
DataTable转为json
DataTable string @H_404_17@ToJson(DataTabledt)
{
Dictionary @H_404_17@< string @H_404_17@,255); line-height:1.5!important">object @H_404_17@> @H_404_17@dic @H_404_17@= @H_404_17@ new @H_404_17@Dictionary @H_404_17@< object @H_404_17@> @H_404_17@();

int @H_404_17@index @H_404_17@= @H_404_17@ 0 @H_404_17@;
foreach @H_404_17@(DataRowdr in @H_404_17@dt.Rows)
{
Dictionary @H_404_17@< object @H_404_17@> @H_404_17@result @H_404_17@= @H_404_17@ foreach @H_404_17@(DataColumndc in @H_404_17@dt.Columns)
{
result.Add(dc.ColumnName,dr[dc].ToString());
}
dic.Add(index.ToString(),result);
index @H_404_17@++ @H_404_17@;
}
return @H_404_17@ToJson(dic);
}
}

猜你在找的Json相关文章