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