Reading and writing of "WP development" JSON data

Source: Internet
Author: User

JSON data can also be processed in WP-RT applications without using other libraries. The main classes are placed in the Windows.Data.Json namespace:

The Ijsonvalue interface acts as a specification for encapsulating JSON data, where the read-only attribute ValueType identifies the type of JSON value by Jsonvaluetype enumeration, such as String, which indicates that the value of the JSON is a literal, number represents a value, and so on.

Classes that implement the Ijsonvalue interface are used to process JSON data, such as Jsonobject representing a single JSON object, Jsonarray representing an array, and so on.

May everyone think I said before is nonsense, indeed quite waste, in fact, these types are not so complex, as long as we write more and play tricks can master.

So, let's try it out with a bunch of examples.

1. Create a JSON array and add elements to it.

            // JSON array            New Jsonarray ();             // adding elements to an array             arr. ADD (Jsonvalue.createnumbervalue (100d));            Arr. ADD (Jsonvalue.createnumbervalue (200d));            Arr. ADD (Jsonvalue.createstringvalue ("hotdog"));             // To represent a JSON array as a string            string jsonstr = arr. Stringify ();            Debug.WriteLine (JSONSTR);

The Stringify method can extract the string representation of a JSON object. The results were as follows:

[100,200, "Hot Dog"]

2. You can also use the Jsonobject class to create a JSON object.

Jsonobject obj =NewJsonobject (); //set the name and value of each fieldObj. Setnamedvalue ("Work No.", Jsonvalue.createstringvalue ("000-410002")); Obj. Setnamedvalue ("name", Jsonvalue.createstringvalue ("Wang Puppy")); Obj. Setnamedvalue ("Age", Jsonvalue.createnumbervalue ( $)); Obj. Setnamedvalue ("Marriage No", Jsonvalue.createbooleanvalue (true)); //extracting a string            stringJsstr =obj.            Stringify (); Debug.WriteLine (JSSTR);

As you know, JSON objects are wrapped in a pair of curly braces, each separated by commas, and the field names and values separated by colons, that is, the field includes a name and a specific value, so the Setnamedvalue method is defined as follows:

void Setnamedvalue (string name, Ijsonvalue value);

The name represents the field name of the JSON object, and value is the value because the value of the JSON can also be a complex type, because the values parameter is defined as the Ijsonvalue type, which is OK for all types that implement ijsonvalue by mouth.

The results from the above example are as follows:

{"Work No.": "000-410002", "name": "Wang Puppy", "age": 45, "Married No":True}

3. Generate JSON data from a string.

You can parse a string that represents a JSON, and then generate the correlation type.

            stringTststr ="{\ "id\": \ "40033201\", \ "name\": \ "Xiao Ming \", \ "city\": \ "Tianjin \"}"; Jsonobject obj=Jsonobject.parse (TSTSTR); //get the values for each field            stringid = obj. Getnamedstring ("ID"); stringname = obj. Getnamedstring ("Name"); stringCity = obj. Getnamedstring (" City"); stringdisplay =string. Format ("Study Number: {0}\n name: {1}\n City: {2}", ID, name, city); Debug.WriteLine (display);

Parse is a static method, passing in a string that represents the JSON data, and the corresponding instance object can be produced. To read the value of a specified field from the JSON data, you can call Getnamedxxx, which is called the Getnamedstring method, as the value you want to get in the previous example is a string type.

The results of the above implementation are as follows:

School Number: 40033201 name: Xiao Ming City: Tianjin

Well, the above several examples are more TMD simple, I 350% believe you can understand, let's get a little BT, look at nested JSON objects.

Jsonobject Winning person =NewJsonobject (); //Child ObjectsJsonobject address =NewJsonobject (); Address. Setnamedvalue ("Province", Jsonvalue.createstringvalue ("Guangdong")); Address. Setnamedvalue ("City", Jsonvalue.createstringvalue ("Shantou")); Address. Setnamedvalue ("Detailed Address", Jsonvalue.createstringvalue ("Don't tell you")); //Child ObjectsJsonarray Winning number =NewJsonarray (); Winning number.            ADD (Jsonvalue.createnumbervalue (5d)); Winning number.            ADD (Jsonvalue.createnumbervalue (27d)); Winning number.            Add (Jsonvalue.createnumbervalue (2d)); Winning number.            ADD (Jsonvalue.createnumbervalue (32d)); //Set fieldWinning people. Setnamedvalue ("name", Jsonvalue.createstringvalue ("Super Bet Saint")); Winning people. Setnamedvalue ("Address", address); Winning people. Setnamedvalue ("Personal Profile", Jsonvalue.createstringvalue ("gambling on the solar system invincible hand")); Winning people. Setnamedvalue ("Winning Numbers", winning numbers); stringstr =winning people.            Stringify (); Debug.WriteLine (str);

The results were as follows:

{"Name": "Super Bet Holy", "address": {"province": "Guangdong", "City": "Shantou", "Detailed Address": "Do not Tell You"}, "Personal profile": "Gambling all over the solar system no rival", "winning number": [5,27,2,32]}

Finally, thank you for watching this issue of the old week bragging program.

Reading and writing of "WP development" JSON data

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.