C# 中使用JSON -DataContractJsonSerializer

C# 中使用JSON -DataContractJsonSerializ<wbr>er</wbr>

C#中使用JSON不需要使用第三方库,使用.NETFramwork3.5自带的System.Runtime.Serialization.Json即可很好的完成JSON的解析。

关于JSON的入门介绍见(首页的图很形象):

http://www.json.org/

一、Using

需要添加引用:System.ServiceModel.Web 和System.Runtime.Serialization,然后使用Using:

using <wbr>System.Runtime.Serialization.Json;<br></wbr> using <wbr>System.Runtime.Serialization;</wbr>

<wbr>二、定义序列化的类</wbr>

假如我们要转化的JSON字符串格式为:

{
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
" encoding " : " UTF-8 " ,
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
" plug-ins " :[ " python " , " c++ " , " ruby " ],
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
" indent " :{
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
" length " : 3 ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
" use_space " : true
<wbr><wbr><wbr><wbr>}<br> }</wbr></wbr></wbr></wbr>

然后编写相应的序列化的类,注意下面类加的Attribute:

C#<wbr>中使用JSON [DataContract(Namespace<wbr></wbr>=<wbr></wbr>"http://coderzh.cnblogs.com")]
class<wbr>Config<br> {<br><wbr><wbr><wbr><wbr>[DataMember(Order<wbr></wbr></wbr></wbr></wbr></wbr></wbr>=<wbr></wbr>0)]
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
public<wbr></wbr>string<wbr>encoding<wbr>{<wbr></wbr></wbr></wbr>get;<wbr></wbr>set;<wbr>}<br><wbr><wbr><wbr><wbr>[DataMember(Order<wbr></wbr></wbr></wbr></wbr></wbr></wbr>=<wbr></wbr>1)]
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
public<wbr></wbr>string[]<wbr>plugins<wbr>{<wbr></wbr></wbr></wbr>get;<wbr></wbr>set;<wbr>}<br><wbr><wbr><wbr><wbr>[DataMember(Order<wbr></wbr></wbr></wbr></wbr></wbr></wbr>=<wbr></wbr>2)]
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
public<wbr>Indent<wbr>indent<wbr>{<wbr></wbr></wbr></wbr></wbr>get;<wbr></wbr>set;<wbr>}<br> }<br><br> [DataContract(Namespace<wbr></wbr></wbr>=<wbr></wbr>"http://coderzh.cnblogs.com")]
class<wbr>Indent<br> {<br><wbr><wbr><wbr><wbr>[DataMember(Order<wbr></wbr></wbr></wbr></wbr></wbr></wbr>=<wbr></wbr>0)]
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
public<wbr></wbr>int<wbr>length<wbr>{<wbr></wbr></wbr></wbr>get;<wbr></wbr>set;<wbr>}<br><wbr><wbr><wbr><wbr>[DataMember(Order<wbr></wbr></wbr></wbr></wbr></wbr></wbr>=<wbr></wbr>1)]
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
public<wbr></wbr>bool<wbr>use_space<wbr>{<wbr></wbr></wbr></wbr>get;<wbr></wbr>set;<wbr>}<br> }</wbr>

三、对象转化为JSON字符串

使用WriteObject方法:

C#<wbr>中使用JSON
var<wbr>config<wbr></wbr></wbr>=<wbr></wbr>new<wbr>Config(){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>encoding<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>=<wbr></wbr>"UTF-8",
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>plugins<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=<wbr></wbr>new<wbr></wbr>string[]{"python",<wbr></wbr>"C++",<wbr></wbr>"C#"},
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>indent<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=<wbr></wbr>new<wbr>Indent(){<wbr>length<wbr></wbr></wbr></wbr>=<wbr></wbr>4,<wbr>use_space<wbr></wbr></wbr>=<wbr></wbr>false}
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>};<br> var<wbr>serializer<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
=<wbr></wbr>new<wbr>DataContractJsonSerializ<wbr>er(</wbr></wbr>typeof(Config));
var<wbr>stream<wbr></wbr></wbr>
=<wbr></wbr>new<wbr>MemoryStream();<br> serializer.WriteObject(stream,<wbr>config);<br><br></wbr></wbr>byte[]<wbr>dataBytes<wbr></wbr></wbr>=<wbr></wbr>new<wbr></wbr>byte[stream.Length];

stream.Position<wbr></wbr>
=<wbr></wbr>0;

stream.Read(dataBytes,<wbr></wbr>
0,<wbr>(</wbr>int)stream.Length);

string<wbr>dataString<wbr></wbr></wbr>=<wbr>Encoding.UTF8.GetString(dataBytes);<br><br> Console.WriteLine(</wbr>"JSON<wbr>string<wbr>is:</wbr></wbr>");
Console.WriteLine(dataString);

四、JSON字符串转对象

使用ReadObject方法:

C#<wbr>中使用JSON
var<wbr>mStream<wbr></wbr></wbr>=<wbr></wbr>new<wbr>MemoryStream(Encoding.Default.GetBytes(dataString));<br> Config<wbr>readConfig<wbr></wbr></wbr></wbr>=<wbr>(Config)serializer.ReadObject(mStream);<br><br> Console.WriteLine(</wbr>"Encoding<wbr>is:<wbr>{0}</wbr></wbr>",<wbr>readConfig.encoding);<br></wbr>foreach<wbr>(</wbr>string<wbr>plugin<wbr></wbr></wbr>in<wbr>readConfig.plugins)<br> {<br><wbr><wbr><wbr><wbr>Console.WriteLine(</wbr></wbr></wbr></wbr></wbr>"plugins<wbr>is:<wbr>{0}</wbr></wbr>",<wbr>plugin);<br> }<br> Console.WriteLine(</wbr>"indent.length<wbr>is:<wbr>{0}</wbr></wbr>",<wbr>readConfig.indent.length);<br> Console.WriteLine(</wbr>"indent.use_space<wbr>is:<wbr>{0}</wbr></wbr>",<wbr>readConfig.indent.use_space);</wbr>

<wbr>五、输出结果:</wbr>

JSON<wbr></wbr> string <wbr></wbr> is :
{
" encoding " : " UTF-8 " , " plugins " :[ " python " , " C++ " , " C# " ], " indent " :{ " length " : 4 , " use_space " : false }}
Encoding<wbr></wbr>
is :<wbr>UTF</wbr> - 8
plugins<wbr></wbr>
is :<wbr>python<br> plugins<wbr></wbr></wbr> is :<wbr>C</wbr> ++
plugins<wbr></wbr>
is :<wbr>C#<br> indent.length<wbr></wbr></wbr> is :<wbr></wbr> 4
indent.use_space<wbr></wbr>
is :<wbr>False</wbr>

你可能感兴趣的:(Serialize)