Json.NET Simple to use

Source: Internet
Author: User
Tags ming

Json. NET (http://json.codeplex.com/) is used to set the. NET objects into JSON strings (serialized?). ), or convert the JSON string to an object of an existing type in. NET (deserialization?). )

First, for example, define a type casually:

public class product{Public    string Name {get; set;}    Public DateTime expiry {get; set;}    Public decimal price {get; set;}    Public string[] Sizes {get; set;}    public override string ToString ()    {        return string. Format ("name:{0},expiry:{1},price:{2},sizescount:{3}"            , Name, expiry, price, sizes.length);    }

Initialize the object:

public static void Main (string[] passwordargs) {    Product product = new Product ()    {        Name = "Android",        Expiry = DateTime.Now, Price        = $,        Sizes = new string[] {"1.5", "2.2", "4.1"}    };}

To convert to JSON:

Console.WriteLine (Jsonconvert.serializeobject (product));

Output Result:

{"Name": "Android", "expiry": "2013-08-30t09:50:11.5147845+08:00", "Price": 2000.0, "Sizes": ["1.5", "2.2", "4.1"]}

Everything else looks normal except for this date a little strange

Formatted Date:

Format date time, same as ToString format for datatime type Isodatetimeconverter ISO = new isodatetimeconverter (); ISO. DateTimeFormat = "Yyyy-mm-dd HH:mm:ss"; Console.WriteLine (Jsonconvert.serializeobject (product, ISO));

Output Result:

{"Name": "Android", "expiry": "2013-08-30 09:53:58", "Price": 2000.0, "Sizes": ["1.5", "2.2", "4.1"]}

Conversion from JSON to object:

String str = "{\" name\ ": \" android\ ", \" expiry\ ": \" 2013-08-30 09:53:58\ ", \" price\ ": 2000.0,\" sizes\ ": [\" 1.5\ ", \" 2.2\ " , \ "4.1\"]} "; Product P = (product) jsonconvert.deserializeobject (str, typeof (product)); Console.WriteLine (P.tostring ());

Output Result:

Name:android,expiry:2013/8/30 9:53:58,price:2000.0,sizescount:3

Conversion from JSON to key-value pairs:

String Strjson = @ "{" "Name1" ":" "Xiao Ming" "," "Name2" ":" "Floret" "," "Name3" ":" "Little Red" "}";D ictionary<string, String> _ Dictionary = jsonconvert.deserializeobject<dictionary<string, string>> (Strjson); foreach (KeyValuePair <string, string> KP in _dictionary) {    Console.WriteLine (KP. Key + ":" + KP. Value);}

Output Result:

Name1: Xiao Ming
Name2: Xiao Hua
Name3: Xiao Hong

Converting from a string to a JSON object, as well as a simple use of a JSON object:

String strJson2 = @ "{" "Student" ": {" "Name1" ":" "Xiao Ming" "," "Name2" ":" "Floret" "," "Name3" ":" "Little Red" "}}"; Jobject jsonobj = Jobject.parse (StrJson2); Console.WriteLine (jsonobj["Student" ["Name1"]. ToString ()); Console.WriteLine (jsonobj["Student" ["Name2"]. ToString ()); Console.WriteLine (jsonobj["Student" ["Name3"]. ToString ());

Output Result:

Xiao ming
Small flower
Little Red

To generate a JSON object directly:

Jobject json =    new Jobject (New    jproperty ("Channel",        new Jobject (New        jproperty ("title", "JSON"), New        Jproperty ("link", "json.net"),        new Jproperty ("description", "Json.NET description"),        new Jproperty ( "Items", New            Jarray (new            jobject ("Haha1", "123"),            new Jobject ("Jproperty", "456" )),            new Jobject (New Jproperty ("Haha3", "789")))))                ; Console.WriteLine (JSON. ToString ());

Output Result:

{
"Channel": {
"title": "JSON",
"Link": "Json.NET",
"description": "Json.NET description",
"Items": [
{
"HAHA1": "123"
},
{
"HAHA2": "456"
},
{
"HAHA3": "789"
}
]
}
}

So much for the time being, and then we'll keep adding.

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.