JSON(JavaScript Object Notation) is a lightweight data exchange format. Essentially, it is no different from formatted data formats such as XML and yaml. It is easy for people to read and write, and easy for machine parsing and generation. It is based onJavaScript programming language,Standard
ECMA-262 3rd edition-December 1999. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language.
JSON is constructed in two structures:
- A collection of name/value pairs ). In different languages, it is understoodObject), Record, structure (struct), Dictionary, hash table, keyed list, or associative array ).
- An ordered list of values ). In most languages, it is understood as an array ).
These are common data structures. In fact, most modern computer languages support them in some form. This makes a data format based on these structuresProgramming LanguageExchange between them becomes possible.
JSON has the following forms:
An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.
An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.
Value (Value) Can be a string enclosed by double quotation marks (String), Value (number ),True
,False
,Null
Object or array ). These structures can be nested.
String (StringIs a set of any number of Unicode characters enclosed by double quotation marks, which are escaped using a backslash. A character (character) is a separate string (character string ).
String (String) Is very similar to a C or Java string.
Value (Number) Is also very similar to the values in C or Java. Remove unused octal and hexadecimal formats. Except for some encoding details.
C ++ uses JSON to parse data. Generally, jsoncpp is used.
Website: http://sourceforge.net/projects/jsoncpp/
After downloading, unzip and open \ jsoncpp-src-0.5.0 \ jsoncpp-src-0.5.0 \ makefiles \ vs71
To obtain a static Link Library JSON. Lib.
To use jsoncpp, just copy this lib file to your project directory and copy the jsoncpp-src-0.5.0 \ jsoncpp-src-0.5.0 \ include \ JSON
Copy the header files to the project directory and add them to the project.
Example:
{
"Name": "ajioy ",
"Age": 22
}
?
123456789101112131415161718192021222324252627 |
# Pragma comment (Lib, "json_mtd.lib ") # Include <fstream> # Include <cassert> # Include "JSON/JSON. H" Int Main () { Ifstream IFS;
IFS. Open ( "Testjson. JSON" ); Assert (IFS. is_open ()); JSON: reader; JSON: Value root; If (! Reader. parse (IFS, root, False
)) { Return -1; } STD: string name = root [ "Name" ]. Asstring (); Int Age = root [
"Age" ]. Asint (); STD: cout <name <STD: Endl; STD: cout <age <STD: Endl; Return 0; } |
[{"Name": "xiaoy", "Age": 17 },{ "name": "xiaot", "Age": 20}]
1234567891011121314151617181920212223242526272829303132 |
# Pragma comment (Lib, "json_mtd.lib ") # Include <fstream> # Include <cassert> # Include "JSON/JSON. H" Int Main () { Ifstream IFS; IFS. Open ( "Testjson. JSON"
); Assert (IFS. is_open ()); JSON: reader; JSON: Value root; If (! Reader. parse (IFS, root, False ))
{ Return -1; } STD: string name; Int Age; Int Size = root. Size ();
For ( Int I = 0; I <size; ++ I)
{ Name = root [I] [ "Name" ]. Asstring (); Age = root [I] [ "Age" ]. Asint (); STD: cout <name < "" <Age <STD: Endl; } Return 0; } |
JSON writing:
123456789101112131415161718192021222324252627 |
# Pragma comment (Lib, "json_mtd.lib ")
# Include <fstream> # Include <cassert> # Include "JSON/JSON. H" Int Main () { JSON: Value root; JSON: fastwriter writer; JSON: Value person;
Person [ "Name" ] = "Hello World" ; Person [ "Age" ] = 100; Root. append (person ); STD: String json_file = writer. Write (Root );
Ofstream OFS; OFS. Open ( "Test1.json" ); Assert (OFS. is_open ()); OFS <json_file;
Return 0; } |
Result: [{"Age": 100, "name": "Hello World"}]
JSON array parsing also supports the STL style. That is
JSON: Value: Members member; // Members is like vector <string> and typedef.
For (JSON: Value: iterator itr = Objarray. Begin (); itr ! = Objarray. End (); itr ++ )
{
Member = ( * Itr). getmembernames ();
For (JSON: Value: Members: iterator ITER = Member. Begin (); ITER ! = Member. End (); ITER ++ )
{
String Str_temp = ( * Itr )[( * ITER)]. asstring ();
}
}
This style is similar to the above, but the above only takes the "text" node, and the next is to output all nodes.
The Application of JSON in C/C ++ and QT parsing JSON instance http://blog.simophin.net /? P = 193