LitJson.dll Download
A simple example:
String str = "{' name ': ' CYF ', ' ID ': Ten, ' Items ': [{' Itemid ': 1001, ' itemname ': ' Hello '},{' itemid ': 1002, ' itemname ': ' Hello2 '}]} ";
Reading data from a JSON string *******************************
Jsondata JD = Jsonmapper.toobject (str);
String name = (string) jd["name"];
Long id = (long) jd["id"];
Jsondata Jditems = jd["items"];
int itemcnt = Jditems.count;
Number of items in array items
foreach (Jsondata item in Jditems)
Iterating through the array items
{int itemID = (int) item["ItemID"];
String itemname = (string) item["ItemName"];
}
Convert Jsondata to JSON string ***************************
String str2 = JD. ToJson ();
Litjson is a class library that processes JSON-formatted data in a. NET platform, small and fast. Its source code is written in C # and can be passed by any. NET platform, currently the latest version is Litjson 0.5.0.
With these several. Litjson's performance is far ahead of the open source JSON library on the net platform:
Jayrock version 0.9.8316
Litjson version 0.3.0
Newtonsoft Json.NET version 1.1
The methods commonly used in Litjson are described below:
The following example needs to add a reference LitJson.dll before importing the namespace using Litjson;
Download
1. Conversion of entity objects in Json and C #
Example 1.1: Transforming Data using the Jsonmapper class
Ublic class Person
{
public string Name {get; set;}
public int Age {get; set;}
Public DateTime Birthday {get; set;}
}
public class Jsonsample
{
public static void Main ()
{
Persontojson ();
Jsontoperson ();
}
///
Convert entity classes to JSON format
///
public static void Persontojson ()
{
Person bill = new person ();
Bill. Name = "www.87cool.com";
Bill. Age = 3;
Bill. Birthday = new DateTime (2007, 7, 17);
String json_bill = Jsonmapper.tojson (Bill);
Console.WriteLine (Json_bill);
Output: {"Name": "Www.87cool.com", "Age": 3, "Birthday": "07/17/2007 00:00:00"}
}
///
Converting JSON data to entity classes
///
public static void Jsontoperson ()
{
String json = @ "
{
"" Name "": "" www.87cool.com "",
"Age" ": 3,
"" Birthday "": "" 07/17/2007 00:00:00 ""
}";
Person Thomas = Jsonmapper.toobject (JSON);
Console.WriteLine ("' 87cool ' Age: {0}", Thomas. Age);
Output: ' 87cool ' Age:3
}
}
Example 1.2: Use the Jsonmapper class to convert a JSON string to a jsondata recognized by C #, and then get its value from the JSON Data's property name or index.
Reading the Jsondata object in C # is exactly the same as reading the JSON in JavaScript;
This kind of reading of JSON is very cool in C #, and it is also very useful, since many of the APIs that are provided by Web applications now return data in JSON format.
The Flickr album API returns data in JSON format.
public static void Loadalbumdata (String json_text)
{
Jsondata data = Jsonmapper.toobject (Json_text);
Console.WriteLine ("Album ' s name: {0}", data["Album" ["name"]);
String artist = (String) data["album" ["Name"];
int year = (int) data["album" ["Year"];
Console.WriteLine ("First track: {0}", data["album" ["Tracks"][0]);
}
2. Readers and writers of Json in C #
example of how to use 2.1:jsonreader class
public class DataReader
{
public static void Main ()
{
String sample = @ "{
"Name" ":" "Bill",
"Age" ": 32,
"" Awake "": true,
"N" ": 1994.0226,
"Note" ": [" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ",
}";
Readjson (sample);
}
Outputs the type and value of all JSON data
public static void Readjson (String json)
{
Jsonreader reader = new Jsonreader (JSON);
Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type");
Console.WriteLine (New String ('-', 42));
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);
}
}
}
Output Result:
JSON type value C # type
//------------------------------------------
Objectstart
PropertyName name System.String
String Bill System.String
PropertyName Age System.String
Int 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
example of how to use 2.2:jsonwriter class
public class DataReader
{
Creating a JSON object from the Jsonwriter class
public static void Writejson ()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
Jsonwriter writer = new Jsonwriter (SB);
Writer. Writearraystart ();
Writer. Write (1);
Writer. Write (2);
Writer. Write (3);
Writer. Writeobjectstart ();
Writer. Writepropertyname ("color");
Writer. Write ("Blue");
Writer. Writeobjectend ();
Writer. Writearrayend ();
Console.WriteLine (sb.) ToString ());
Output: [1,2,3,{"Color": "Blue"}]
}
}
. NET Platform Open source JSON library Litjson how to use