Json learning notes

Source: Internet
Author: User
Json has the biggest advantage over Xml, based on strings. Xml must be related to files, while json is only a string (of course file-related operations are also provided ). Let & amp; #39; ssayitfrommycode: [cpp] # include & lt; json/json. h & gt; // extract the package and find the object... syntax Json has the biggest advantage over Xml, based on strings. Xml must be related to files, while json is only a string (of course file-related operations are also provided ). Let's say it from my code: [cpp] # include // Decompress the package and find the directory vs71. Use vs to open the package and generate a solution. This program is directly rewritten in jsontest. cpp # include # Include # Include Using namespace std; const string g_file = "json. c "; // originally thought vs would make a format for the json format string after opening the file. The result is the same as opening the file in Notepad. It is better to have a clear xml structure. // the two structures are not used here, their purpose is to let us see the json string format struct Address {string name; // street name int number; // Street number}; struct Student {int no; // learn string name; // name Address addr; // home Address}; void Write () {Json: Value root; // root (like the root of a fruit tree) int no [] = {2008,201 0, 2013}; string name [] = {"sumos", "fly away", "sun" }; String name2 [] = {"Xihu road", "Donghu road", "Zhongnanhai"}; int number [] = {101,202,303}; for (int k = 0; k <3; k ++) {Json: Value person, addr; person ["no"] = Json: Value (no [k]); person ["name"] = Json: Value (name [k]); addr ["name"] = Json: Value (name2 [k]); addr ["number"] = Json: Value (number [k]); person ["address"] = addr; root. append (person);} Json: FastWriter writer; // FastWriter does not have Encode ofstream out; out. Open (g_file); if (out. is_open () {out <writer. write (root); out. close () ;}} void Read () {ifstream in; in. open (g_file); if (! In. is_open () return; Json: Reader reader; Json: Value root; bool r = reader. parse (in, root); if (! R) {in. close (); return;} int n = root. size (); for (int k = 0; k <n; k ++) {Json: Value person = root [k]; cout <person ["no"]. asInt () <person ["name"]. asString () <

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.