Author: Wang Xuan Yi, source: http://www.cnblogs.com/neverdie/Reprinted, Please keep this statement. If you like this article, click [recommendation ]. Thank you!
Using LitJson; using System; public class Person {// C #3.0 auto-implemented properties public string Name {get; set;} public int Age {get; set ;} public DateTime Birthday {get; set ;}} public class JsonSample {public static void Main () {PersonToJson (); JsonToPerson ();} public static void PersonToJson () {Person bill = new Person (); bill. name = "William Shakespeare"; bill. age = 51; bill. birthday = new DateTime (1564, 4, 26); string json_bill = JsonMapper. toJson (bill); Console. writeLine (json_bill);} public static void JsonToPerson () {string json = @ "{" "Name" ":" "Thomas More" "," "Age" ": 57, "" Birthday ":" "02/07/1478 00:00:00"} "; Person thomas = JsonMapper. toObject <Person> (json); Console. writeLine ("Thomas 'Age: {0}", thomas. age );}}
Output above:
{"Name":"William Shakespeare","Age":51,"Birthday":"04/26/1564 00:00:00"}Thomas' age: 57
Use non-generic JsonMapper. ToObject
If no particular JSON data class exists, it returns a JSONData instance. JSONData is a general type that can store JSON data of any data type, including list and dictionary.
using LitJson;using System;public class JsonSample{ public static void Main() { string json = @" { ""album"" : { ""name"" : ""The Dark Side of the Moon"", ""artist"" : ""Pink Floyd"", ""year"" : 1973, ""tracks"" : [ ""Speak To Me"", ""Breathe"", ""On The Run"" ] } } "; LoadAlbumData(json); } public static void LoadAlbumData(string json_text) { Console.WriteLine("Reading data from the following JSON string: {0}", json_text); JsonData data = JsonMapper.ToObject(json_text); // Dictionaries are accessed like a hash-table Console.WriteLine("Album's name: {0}", data["album"]["name"]); // Scalar elements stored in a JsonData instance can be cast to // their natural types string artist = (string) data["album"]["artist"]; int year = (int) data["album"]["year"]; Console.WriteLine("Recorded by {0} in {1}", artist, year); // Arrays are accessed like regular lists as well Console.WriteLine("First track: {0}", data["album"]["tracks"][0]); }}
Output of the above example:
Reading data from the following JSON string: { "album" : { "name" : "The Dark Side of the Moon", "artist" : "Pink Floyd", "year" : 1973, "tracks" : [ "Speak To Me", "Breathe", "On The Run" ] } }Album's name: The Dark Side of the MoonRecorded by Pink Floyd in 1973First track: Speak To Me
Reader and Writer
Some people like to use stream to process JSON data. For them, the interfaces we provide are jsonreader and jsonwriter.
JSONMapper is actually based on the above two classes, so you can regard these two classes as the underlying interfaces of litJSON.
Use JsonReader
using LitJson;using System;public class DataReader{ public static void Main() { string sample = @"{ ""name"" : ""Bill"", ""age"" : 32, ""awake"" : true, ""n"" : 1994.0226, ""note"" : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ] }"; PrintJson(sample); } public static void PrintJson(string json) { JsonReader reader = new JsonReader(json); Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type"); Console.WriteLine (new String ('-', 42)); // The Read() method returns false when there's nothing else to read while (reader.Read()) { string type = reader.Value != null ? reader.Value.GetType().ToString() : ""; Console.WriteLine("{0,14} {1,10} {2,16}", reader.Token, reader.Value, type); } }}
The output is as follows:
Token Value Type------------------------------------------ ObjectStart PropertyName name System.String String Bill System.String PropertyName age System.String Int 32 System.Int32 PropertyName awake System.String Boolean True System.Boolean PropertyName n System.String Double 1994.0226 System.Double PropertyName note System.String ArrayStart String life System.String String is System.String String but System.String String a System.String String dream System.String ArrayEnd ObjectEnd