C + + from string to Json/json to string/array parsing/array additions

Source: Internet
Author: User
Tags string to json

String into JSON (where Str is a string)

Json::reader Reader; Json::value Devjson; Reader.parse (str,devjson); int dev_id = devjson["dev_id"].asint (); int index = devjson["index"].asint ();

  

JSON-to-string (where Devstr is a string)

Json::value Devjson = Devsjson[i]; std::string devstr = devjson.tostyledstring (); printf ("msg:%s", Devstr.c_str ());

  

JSON Array parsing:

Json::reader Reader; Json::value Devsjson; Reader.parse (MSGSTR, Devsjson); int sinum = Devsjson.size (); for (int i=0; i < sinum; i++) {Json::value Devjson = devsjson[i]; std::string devstr = devjson.tostyledstring (); printf ("msg:%s", Devstr.c_str ()); }

  

Array additions:

Json::value Root; Json::value person; person["name"] = "Hello World"; Person["age"] = 100; Root.append (person);

  

Result: [{"Age": +, "name": "Hello World"}]

---------------------------------------------------

MyTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "iostream" #include "time.h" #include "map" #include <windows.h> #include < sstream> #include <list> #include "json\json.h" #include "stdint.h" using namespace std;int _tmain (int argc, _ tchar* argv[]) {json::value value;uint32_t ssrc1 = 5305490;value["Ssrc1"] = ssrc1;uint32_t Ssrc2 = 2152748638;value[" Ssrc2 "] = ssrc2;std::string BODY = value.tostyledstring (); cout << body << Endl; Json::reader Reader; Json::value Data;reader.parse (body, data, false), if (Data.ismember ("Ssrc1")) {cout << "Ssrc1"; if (data["Ssrc1"]. Isint ()) cout << ' is Int ' << endl;if (data["Ssrc1"].isuint ()) cout << "is UInt" << Endl; if (Data.ismember ("Ssrc2")) {cout << "ssrc2"; if (data["Ssrc2"].isint ()) cout << "is Int" << endl;if (Dat a["Ssrc2"].isuint ()) cout << "is UInt" << Endl;} uint32_t ssrc11 = data["Ssrc1"].asuint () uint32_t ssrc21 = data["Ssrc2"].asuint () cout << "SSRC11:" << ssrc11 << endl;cout << "ssrc21:" << ssrc21 << endl;system ("pause"); return 0;} 

Output Result:

C + + Json/json to string/array parsing/array addition

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.