Json array parsing instance

Source: Internet
Author: User
Tags string to json

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
  1. # Pragma once
  2. # Pragma comment (lib, "json_vc71_libmtd.lib ")
  3. # Include "json/json. h"

    Implementation

    [Cpp]View plaincopy
    1. Void CJSONTestDlg: OnBnClickedButton1 ()
    2. {
    3. CEdit * pEdit = (CEdit *) GetDlgItem (IDC_EDIT1 );
    4. CString str;
    5. PEdit-> GetWindowText (str); // str is the JSON string defined in)
    6. PEdit-> FmtLines (true );
    7. Json: Reader reader;
    8. Json: Value root;
    9. 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.
    10. {
    11. Std: string JsonID = root ["JsonID"]. asString ();
    12. Json: Value rtnRoot;
    13. RtnRoot ["JsonID"] = JsonID;
    14. Json: Value ack;
    15. Json: Value send = root ["Send"];
    16. If (! Send ["Items"]. isNull ()){
    17. Json: Value Items = send ["Items"];
    18. Int sendSize = Items. size ();
    19. For (int I = 0; I Std: string Code = Items [I] ["Code"]. asString ();
    20. Int x = Items [I] ["X"]. asInt ();
    21. Int y = Items [I] ["Y"]. asInt ();
    22. Int count = Items [I] ["Count"]. asInt ();
    23. // The content required to generate a value in a more read JSON string
    24. Json: Value newAckItem;
    25. NewAckItem ["Code"] = Code;
    26. NewAckItem ["ActualCount"] = count;
    27. Ack. append (newAckItem );
    28. }
    29. }
    30. RtnRoot ["Ack"] = ack;
    31. Std: string rtnOut = rtnRoot. toStyledString (); // generate a JSON string with a format
    32. # Ifdef UNICODE
    33. Std: wstring stemp = s2ws (rtnOut );
    34. LPCWSTR result = stemp. c_str ();
    35. # Else
    36. LPCWSTR result = rtnOut. c_str ();
    37. # Endif
    38. MessageBox (result, _ T ("corresponding JSON string information generated based on the JSON string "));
    39. CEdit * pEdit = (CEdit *) GetDlgItem (IDC_EDIT2 );
    40. PEdit-> SetWindowText (result );
    41. } Else {
    42. CEdit * pRtnEdit = (CEdit *) GetDlgItem (IDC_EDIT2 );
    43. PRtnEdit-> SetWindowText (_ T ("JSON format error "));
    44. }
    45. }

      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

Related Article

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.