Json array parsing instance
1. Introduction to JsonCPP
Jsoncpp is one of the common parsing libraries for c ++ to parse JSON strings. Common classes include:
A) Json: Value can represent all types in, such as int, string, object, array, etc. For supported types, see values in Json: ValueType.
B) Json: Reader parses the json file stream or string to Json: Value. The main function is Parse.
C) Json: Writer is the opposite of Json: Reader. It converts Json: Value into a string stream. Note that it has two sub-classes: Json: FastWriter and Json: StyleWriter, output json without format and json with format respectively.
D) Json: Value: Members is mainly used to parse JSON arrays in STL style. People who have read the source code know that Members is actually a typedefvector. .
2. JSONCPP parsing example
A) Parse JSON string format
{
"JsonID": "BD6D7FDA-54D2-468b-A3DE-9D5FBDB78207 ",
"Send ":{
"ID": "B8E09E97-F379-4bb0-814A-389FD3F66631 ",
"Items ":[
{
"Count": 2,
"Code": "0101 ",
"X": 1,
"Y": 2
},
{
"Count": 2,
"Code": "0101 ",
"X": 1,
"Y": 2
}
]
}
}
B) generated JSON string
{
"Ack ":[
{
"ActualCount": 2,
"Code": "0101"
},
{
"ActualCount": 2,
"Code": "0101"
}
],
"JsonID": "BD6D7FDA-54D2-468b-A3DE-9D5FBDB78207"
}
C) Parse and generate JSON code
. H file to be introduced
[Html]View plaincopy
- # Pragma once
- # Pragma comment (lib, "json_vc71_libmtd.lib ")
-
- # Include "json/json. h"
Implementation
[Cpp]View plaincopy
- Void CJSONTestDlg: OnBnClickedButton1 ()
- {
- CEdit * pEdit = (CEdit *) GetDlgItem (IDC_EDIT1 );
- CString str;
- PEdit-> GetWindowText (str); // str is the JSON string defined in)
- PEdit-> FmtLines (true );
-
- Json: Reader reader;
- Json: Value root;
-
- If (reader. parse (WC2UT (str. GetBuffer (0), root) // reader parses the Json string to the root, and the root contains all the child elements in the Json.
- {
- Std: string JsonID = root ["JsonID"]. asString ();
- Json: Value rtnRoot;
- RtnRoot ["JsonID"] = JsonID;
-
- Json: Value ack;
- Json: Value send = root ["Send"];
- If (! Send ["Items"]. isNull ()){
- Json: Value Items = send ["Items"];
- Int sendSize = Items. size ();
-
- For (int I = 0; I Std: string Code = Items [I] ["Code"]. asString ();
- Int x = Items [I] ["X"]. asInt ();
- Int y = Items [I] ["Y"]. asInt ();
- Int count = Items [I] ["Count"]. asInt ();
-
- // The content required to generate a value in a more read JSON string
- Json: Value newAckItem;
- NewAckItem ["Code"] = Code;
- NewAckItem ["ActualCount"] = count;
-
- Ack. append (newAckItem );
- }
- }
- RtnRoot ["Ack"] = ack;
- Std: string rtnOut = rtnRoot. toStyledString (); // generate a JSON string with a format
- # Ifdef UNICODE
- Std: wstring stemp = s2ws (rtnOut );
- LPCWSTR result = stemp. c_str ();
- # Else
- LPCWSTR result = rtnOut. c_str ();
- # Endif
- MessageBox (result, _ T ("corresponding JSON string information generated based on the JSON string "));
- CEdit * pEdit = (CEdit *) GetDlgItem (IDC_EDIT2 );
- PEdit-> SetWindowText (result );
- } Else {
- CEdit * pRtnEdit = (CEdit *) GetDlgItem (IDC_EDIT2 );
- PRtnEdit-> SetWindowText (_ T ("JSON format error "));
- }
- }
Import JSONCPP as a static library. Note that the Runtime Library in the Code Generation in the project must be consistent with the Runtime Library generated by the code in the static library project of JSONCPP. Otherwise, the following error will be reported:
Afxver _. h (81): fatal error C1189: # error: Please use the/MD switch for _ AFXDLL builds
The Runtime Library in the project must be set the same