Newtonsoft.json Code

Source: Internet
Author: User

Serialization of

Product Product = new product ();p roduct. Expirydate = new DateTime (2008, 12, 28); Jsonserializer serializer = new Jsonserializer (); serializer. Converters.add (New Javascriptdatetimeconverter ()); serializer. nullvaluehandling = nullvaluehandling.ignore;using (StreamWriter sw = new StreamWriter (@ "C:\json.txt")) using ( Jsonwriter writer = new JsonTextWriter (sw)) {    serializer. Serialize (writer, product);    {"Expirydate": New Date (1230375600000), "Price": 0}}

Jsonreader Read JSON string

string @" {"Input" ":" "Value" "," "Output" ":" "Result" "} "  New jsontextreader (new  StringReader (Jsontext));  while (reader. Read ()) {    "\t\t""\t\t" + reader. Value);}

Jsonwriter Writing strings

StringWriter SW =NewStringWriter (); Jsonwriter writer=NewJsonTextWriter (SW); writer. Writestartobject (); writer. Writepropertyname ("input"); writer. WriteValue ("value"); writer. Writepropertyname ("Output"); writer. WriteValue ("result"); writer. Writeendobject (); writer. Flush ();stringJsontext =SW. Getstringbuilder (). ToString (); Console.WriteLine (jsontext);

Reading and writing strings using Jobject

Jobject Jo = Jobject.parse (Jsontext); string[] values = Jo. Properties (). Select (item = Item. Value.tostring ()). ToArray ();

Using Newtonsoft.json;

Using Newtonsoft.Json.Linq;

String jsontext = "[{' A ': ' AAA ', ' B ': ' BBB ', ' C ': ' CCC '},{' a ': ' Aaa2 ', ' B ': ' Bbb2 ', ' C ': ' CCC2 '}]";

Jarray ja = (jarray) jsonconvert.deserializeobject (Jsontext);

Jobject o = (jobject) ja[1];

Console.WriteLine (o["a"]);

Console.WriteLine (ja[1]["a"]);

Jsonserializer read-write objects (based on Jsonwriter and Jsonreader)

 string  jsonArrayText1 =  " [{' A ': ' A1 ', ' B ': ' B1 '},{' a ': ' A2 ', ' B ': ' B2 '}]    = (Jarray) jsonconvert.deserializeobject (JSONARRAYTEXT1);  string  ja1a = Ja[ " a  "   //  or  Jobject o = (jobject) ja[";  string  oa = O[ " a   "]. ToString (); 

Nested formats

String Jsontext ={\ "beijing\": {\ "zone\": \ "Haidian \", \ "zone_en\": \ "Haidian\"}}"; Jobject Jo = (jobject) jsonconvert.deserializeobject (jsontext);  String zone = jo["Beijing" ["zone"]. ToString (); string zone_en = jo["Beijing" ["zone_en"]. ToString ();                   

Custom class Project

Class project{Public    string Input {get; set;}    public string Output {get; set;}}

Project P = new Project () {Input = "stone", Output = "Gold"}; Jsonserializer serializer = new Jsonserializer (); StringWriter SW = new StringWriter (); serializer. Serialize (new JsonTextWriter (SW), p); Console.WriteLine (SW. Getstringbuilder (). ToString ()); StringReader sr = new StringReader (@ "{" "Input" ":" "Stone" "," "Output" ":" "Gold" "}"); Project P1 = (project) serializer. Deserialize (new JsonTextReader (SR), typeof (Project)); Console.WriteLine (P1. Input + "=" + p1. Output);

contractual approach : using DataContractJsonSerializer or jsonreaderwriterfactory provided by System.Runtime.Serialization.dll to achieve

Project p =NewProject () {Input ="Stone", Output ="Gold"};D Atacontractjsonserializer Serializer=NewDataContractJsonSerializer (P.gettype ());stringJsontext;using(MemoryStream stream =NewMemoryStream ()) {Serializer.    WriteObject (Stream, p); Jsontext=Encoding.UTF8.GetString (stream.    ToArray ()); Console.WriteLine (Jsontext);}using(MemoryStream ms =NewMemoryStream (Encoding.UTF8.GetBytes (jsontext))) {DataContractJsonSerializer Serializer1=NewDataContractJsonSerializer (typeof(Project)); Project P1=(Project) serializer1.    ReadObject (MS); Console.WriteLine (P1. Input+"="+P1. Output);}

Note here that the project classes and Members here are related to the attribute:

[DataContract] class project{    [DataMember]    publicstringgetset;    [DataMember]      Public string Get Set ; }}

Practical Reference:
JSON validation Tool: http://jsonlint.com/
JSON Concise Tutorial: http://www.w3school.com.cn/json/
Newtonsoft.json class Library Download: http://json.codeplex.com/

Newtonsoft.json Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.