What is JSON? It's no stranger to everyone. I guess someone knows better than me. Actually, I only know a little bit about this stuff, right, it's fur, skin, and hair, check whether the JSON object is like this?
There is a pair of braces ({}) on the outside. There are many hairs in it. In fact, a JSON object is like a dictionary set with keys and values. Of course, there can be no keys. You see, a standard JSON object is like this.
{
'Key name': key value,
'Key name': key value,
'Key name': Key Value
}
If it is a set, such as an array, put multiple objects in [...], separated by commas (,). Remember to use commas.
Case 1: produce a JSON object from a JSON string
This example shows how to generate a JSON object from a JSON string.
1. Create a blank page with the "Panel" style application.
2. Press Ctrl + v xaml to display the homepage layout.CodeOr someone else said, "This has a hairy relationship with Win8. Isn't it the XAML in WPF ?"
<Page X: class = "app3.mainpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Local = "using: app3" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D"> <page. resources> <style X: Key = "tbstyle" targettype = "textblock"> <setter property = "fontsize" value = "25"/> </style> </page. resources> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel margin = "20"> <button content = "an object generated from a JSON string" Click = "onclick"/> <grid margin = "5, 19, 6, 0 "> <grid. rowdefinitions> <rowdefinition Height = "Auto"/> <rowdefinition Height = "Auto"/> </ grid. rowdefinitions> <grid. columndefinitions> <columndefinition width = "Auto"/> <columndefinition width = "Auto"/> </grid. columndefinitions> <textblock grid. row = "0" grid. column = "0" text = "No.:" style = "{staticresource tbstyle}"/> <textblock X: Name = "tbno" grid. column = "1" grid. row = "0" style = "{staticresource tbstyle}"/> <textblock grid. column = "0" grid. row = "1" text = "name:" style = "{staticresource tbstyle}"/> <textblock X: Name = "tbname" grid. row = "1" grid. column = "1" style = "{staticresource tbstyle}"/> <textblock grid. column = "0" grid. row = "2" text = "City:" style = "{staticresource tbstyle}"/> <textblock X: Name = "tbcity" grid. row = "2" grid. column = "1" style = "{staticresource tbstyle}"/> <textblock grid. row = "3" grid. column = "0" text = "Age:" style = "{staticresource tbstyle}"/> <textblock X: Name = "tbage" grid. row = "3" grid. column = "1" style = "{staticresource tbstyle}"/> </GRID> </stackpanel> </GRID> </Page>
3. The background code is mainly used to process button click events. I will paste a complete post.
Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. data. JSON; namespace app3 {// <sum Mary> // you can use it to navigate to a blank page in the frame. /// </Summary> Public sealed partial class mainpage: Page {public mainpage () {This. initializecomponent ();} private void onclick (Object sender, routedeventargs e) {// JSON string jsonstring = "{" + "\" NO \ ": \" 000-2145 \", "+" \ "Name \": \ "Xiao Wang \", "+" \ "city \": \ "Tianjin \", "+" \ "Age \": 185 "+"} "; // generate the JSON object jsonobject OBJ = jsonobject. parse (jsonstring); // retrieves data from the JSON object this. tbno. TEXT = obj. getnamedstring ("no"); this. tbname. TEXT = obj. getnamedstring ("name"); this. tbcity. TEXT = obj. getnamedstring ("city"); this. tbage. TEXT = obj. getnamednumber ("Age "). tostring ();}}}
From the code, you will see that to process JSON-related data, first introduce the windows. Data. JSON namespace.
Because our JSON object has a naming key, after jsonobject is generated, you can call the following method to obtain the value of the corresponding field:
[JSON is nothing more than a string, Boolean Type, value, array or composite object]
Getnamedarray -- if the corresponding field is a JSON object array, you can call this method.
Getnamedboolean, getnamednumber, getnamedstring, and getnamedobject -- these methods are simple. They all retrieve a single value of the specified type.
Click the button at the top of the page to view the result.
Case 2: convert a JSON object to a string
In this example, the JSON object is converted into a string representation.
1. Create an applicationProgramProject.
2. the UI of mainpage. XAML is as follows:
<Page X: class = "app1.mainpage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns: Local = "using: app1" xmlns: D = "http://schemas.microsoft.com/expression/blend/2008" xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006" MC: ignorable = "D"> <grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel margin = "20"> <button content = "generate JSON string" Click = "onclick "/> <textbox X: name = "txtjson" margin = "8, 17, 8, 0 "Height =" 400 "textwrapping =" Wrap "fontsize =" 20 "/> </stackpanel> </GRID> </Page>
Click the button to generate a JSON object and display the converted string in the textbox below.
3. Process button click events. Remember to introduce the windows. Data. JSON namespace.
Private void onclick (Object sender, routedeventargs e) {// create the first team member object jsonobject menb1 = new jsonobject (); menb1.setnamedvalue ("name", jsonvalue. createstringvalue ("Liu"); menb1.setnamedvalue ("Personality Index", jsonvalue. createnumbervalue (250); menb1.setnamedvalue ("", jsonvalue. createbooleanvalue (true); jsonobject menb2 = new jsonobject (); menb2.setnamedvalue ("name", jsonvalue. createstringvalue ("Lao Zhao"); menb2.setnamedvalue ("Personality Index", jsonvalue. createnumbervalue (97); menb2.setnamedvalue ("", jsonvalue. createbooleanvalue (false); jsonarray arr1 = new jsonarray (); arr1.add (menb1); arr1.add (menb2); // jsonobject teama = new jsonobject (); teama. setnamedvalue ("team name", jsonvalue. createstringvalue ("Ox B Team One"); teama. setnamedvalue ("member list", arr1); // member // jsonobject menbb1 = new jsonobject (); menbb1.setnamedvalue ("name", jsonvalue. createstringvalue ("yellow"); menbb1.setnamedvalue ("Character index", jsonvalue. createnumbervalue (108); menbb1.setnamedvalue ("", jsonvalue. createbooleanvalue (false); jsonobject menbb2 = new jsonobject (); menbb2.setnamedvalue ("name", jsonvalue. createstringvalue ("Xiaolong"); menbb2.setnamedvalue ("Personality Index", jsonvalue. createnumbervalue (392); menbb2.setnamedvalue ("", jsonvalue. createbooleanvalue (true); jsonobject menbb3 = new jsonobject (); menbb3.setnamedvalue ("name", jsonvalue. createstringvalue ("Old yuan"); menbb3.setnamedvalue ("Personality Index", jsonvalue. createnumbervalue (65); menbb3.setnamedvalue ("", jsonvalue. createbooleanvalue (false); jsonarray arr2 = new jsonarray (); arr2.add (menbb1); arr2.add (menbb2); arr2.add (menbb3 ); // Second Team Information: jsonobject teamb = new jsonobject (); teamb. setnamedvalue ("team name", jsonvalue. createstringvalue ("niub second"); teamb. setnamedvalue ("member list", arr2); // put the information of the two teams in an array. jsonarray teams = new jsonarray (); teams. add (teama); teams. add (teamb); // convert the preceding JSON object to the string this.txt JSON. TEXT = teams. stringify ();}
The code above demonstrates the basic information of the two niub teams, and each team contains information about its members.
The result is as follows:
Do you think this method of generating JSON is complicated and painful? Don't worry. In the next blog, we will play some simple things.