DoNet2.0 need the help of Newtonsoft.Json.dll
1 Code2 3 usingSystem;4 usingSystem.IO;5 usingSystem.Text;6 usingNewtonsoft.json;7 8 namespaceOfflineacceptcontrol.uctools9 {Ten Public classJsontools One { A //generating a JSON string from an object information - Public Static stringObjecttojson (Objectobj) - { the returnjavascriptconvert.serializeobject (obj); - } - //generating object information from a JSON string - Public Static ObjectJsontoobject (stringJsonstring,Objectobj) + { - returnjavascriptconvert.deserializeobject (jsonstring, obj. GetType ()); + } A } at}
Donet3.5 comes with DLL processing JSON string
Note reference: System.runtime.serialization,system.servicemodel.web
1 Code2 3 usingSystem;4 usingSystem.Collections.Generic;5 usingSystem.IO;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Runtime.Serialization;9 usingSystem.Runtime.Serialization.Json;Ten One namespaceCrjiiofflineaccept.crjiitools A { - Public classJsontools - { the //generating a JSON string from an object information - Public Static stringObjecttojson (Objectobj) - { -DataContractJsonSerializer serializer =NewDataContractJsonSerializer (obj. GetType ()); +MemoryStream stream =NewMemoryStream (); - Serializer. WriteObject (stream, obj); + byte[] Databytes =New byte[Stream. Length]; AStream. Position =0; atStream. Read (Databytes,0, (int) stream. Length); - returnEncoding.UTF8.GetString (databytes); - } - //generating object information from a JSON string - Public Static ObjectJsontoobject (stringJsonstring,Objectobj) - { inDataContractJsonSerializer serializer =NewDataContractJsonSerializer (obj. GetType ()); -MemoryStream Mstream =NewMemoryStream (Encoding.UTF8.GetBytes (jsonstring)); to returnSerializer. ReadObject (mstream); + } - } the}
C # objects and JSON strings are converted to each other