C# 中使用JSON - DataContractJsonSerializer

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

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

http://www.json.org/ ITPUB个人空间+JqJ%NrvO Ig

一、Using

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

using  System.Runtime.Serialization.Json;
e`�qIT/d0
using  System.Runtime.Serialization;
 二、定义序列化的类

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

4N^4U2y(kP6b$~0
YF2z1Z&F3Sw3x0 Code highlighting produced by Actipro CodeHighlighter (freeware)

{
Z e�F2tn#g$d/0     
" encoding " : " UTF-8 " ,ITPUB个人空间#^h/@W9d_
    
" plug-ins " :[ " python " , " c++ " , " ruby " ],ITPUB个人空间(?(en b X5s
    
" indent " :{ITPUB个人空间I$b;d"@} e1nS
        
" length " : 3 ,
-Tys-d"h o:J_p0         
" use_space " : true ITPUB个人空间"l!r%Y wnt:J
    }ITPUB个人空间 A f9I;NTYW~
}

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

[DataContract(Namespace  =   " http://coderzh.cnblogs.com " )]
CLK;qgd0
class  Config
WG4w9{ UT�O-v0 {
4kT}{)}+vy�F"JW(S/e0     [DataMember(Order 
=   0 )]
V-Ls1]:V9/0     
public   string  encoding {  get set ; }ITPUB个人空间"Av2T$e9c)Zj4Z%O
    [DataMember(Order 
=   1 )]ITPUB个人空间A#m S:w^o
    
public   string [] plugins {  get set ; }
)gn CrCu3u8{9O7A;Ipt0     [DataMember(Order 
=   2 )]
S�vgp$A0     
public  Indent indent {  get set ; }ITPUB个人空间%h0oW|7G#P
}
1GP:O8i6B5h XI0 ITPUB个人空间Z7|3r)X�b-{!P$md
[DataContract(Namespace 
=   " http://coderzh.cnblogs.com " )]ITPUB个人空间$x6W+{s#__
class  Indent
$MO2ev S P0 {
/yR'O~Rr1z0     [DataMember(Order 
=   0 )]
,J/H(G+T`.r:ncu0     
public   int  length {  get set ; }ITPUB个人空间 ^%sm/3ur'J!r*S8y:v
    [DataMember(Order 
=   1 )]ITPUB个人空间eTD'uZcE
    
public   bool  use_space {  get set ; }
t/g$h:H'g#mk`~1Uc0 }
三、对象转化为JSON字符串

使用WriteObject方法:
wX'E4g7s2AdVx1i0

ITPUB个人空间#z4N3Np3?,{(W xe-k
zY%V/I`)Haw3m0 ITPUB个人空间~}d2Lqk
var config  =   new  Config(){ITPUB个人空间p z1mb Q,D~;JI
                         encoding 
=   " UTF-8 " ,ITPUB个人空间{:b AVAs2r?I
                         plugins 
=   new   string []{ " python " " C++ " " C# " },ITPUB个人空间Ai6z ~d
                         indent 
=   new  Indent(){ length  =   4 , use_space  =   false }ITPUB个人空间bb2xbz)?n2r0z.K
                         };ITPUB个人空间&V1w8B!KU]So#{e+/
var serializer 
=   new  DataContractJsonSerializer( typeof (Config));ITPUB个人空间J:DN2g]M&H
var stream 
=   new  MemoryStream();
+/+h2P J-t0 serializer.WriteObject(stream, config);ITPUB个人空间 KX&s0k#H} l
ITPUB个人空间/MU(Wfmo4wi
byte [] dataBytes  =   new   byte [stream.Length];
dXc_LF0
}k a:Yp"U%y]H0 stream.Position 
=   0 ;
d-{&F1s KI[+k0 ITPUB个人空间!dO#I _ yWb
stream.Read(dataBytes, 
0 , ( int )stream.Length);
J�mROON0
G&JYn GW4H D0
string  dataString  =  Encoding.UTF8.GetString(dataBytes);ITPUB个人空间b._[zWo0m
ITPUB个人空间!kO&C2u5v"y(A
Console.WriteLine(
" JSON string is: " );
^A#x!LADR0 Console.WriteLine(dataString);
四、JSON字符串转对象

使用ReadObject方法:

var mStream  =   new  MemoryStream(Encoding.Default.GetBytes(dataString));ITPUB个人空间!Q$ig,m1U+H;jZ
Config readConfig 
=  (Config)serializer.ReadObject(mStream);
~4pp;cw|s0 ITPUB个人空间}Ap,p ke
Console.WriteLine(
" Encoding is: {0} " , readConfig.encoding);ITPUB个人空间`.Y?1Q*G$Ir
foreach  ( string  plugin  in  readConfig.plugins)ITPUB个人空间3L8l't/U B)J)]
{
8Z#|H7d*x,Y2[0     Console.WriteLine(
" plugins is: {0} " , plugin);ITPUB个人空间3oH1C)y h(`xL
}
f/9N:PH@(t0 Console.WriteLine(
" indent.length is: {0} " , readConfig.indent.length);ITPUB个人空间y&MRo'G$oM
Console.WriteLine(
" indent.use_space is: {0} " , readConfig.indent.use_space);

 五、输出结果:
JSON  string   is :
5MM]:VHI0 {
" encoding " : " UTF-8 " , " plugins " :[ " python " , " C++ " , " C# " ], " indent " :{ " length " : 4 , " use_space " : false }}ITPUB个人空间b8K"h7u#A[X$S
Encoding 
is : UTF - 8 ITPUB个人空间 b w @niT,E
plugins 
is : python
}tx�_oy!O ^0 plugins 
is : C ++
+}-A$Jp0qJ0 plugins 
is : C#ITPUB个人空间0s7n:e$n0D'jl
indent.length 
is 4
vx*y3J ~4oY{0 indent.use_space 
is : False

9a9gk)U v-n%r2P0
'AwMn&V%f^%}G
h6Y;wC hatY
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值