1 using System;
2 using System.Text;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Json;
5 using System.IO;
6 7 8 namespace ConsoleApplication1
9 {
10 class Program
11 {
12 static void Main(
string[] args)
13 {
14 string jsonStr =
"{\"id\":1,\"@value\":\"this a @\",\"$p\":\"my name a $\"}";
15 DataContractJsonSerializer serializer =
new DataContractJsonSerializer(
typeof(jtest));
16 17 //反序列化
18 jtest test = serializer.ReadObject(
new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
as jtest;
19 Console.WriteLine( test.value );
20 21 //序列化
22 var stream =
new MemoryStream();
23 serializer.WriteObject(stream, test);
24 byte[] dataBytes =
new byte[stream.Length];
25 stream.Position =
0;
26 stream.Read(dataBytes,
0, (
int)stream.Length);
27 string dataString = Encoding.UTF8.GetString(dataBytes);
28 29 Console.WriteLine(dataString);
30 Console.ReadKey();
31 }
32 }
33 [DataContract]
34 public class jtest
35 {
36 [DataMember(Order =
0)]
37 public int id {
get;
set; }
38 [DataMember(Order =
1, Name =
"@value")]
39 public string value {
get;
set; }
40 [DataMember(Order =
2, Name =
"$p")]
41 public string _p {
get;
set; }
42 }
43 }