Jsoncpp is a cross-platform open Source library that:http://sourceforge.net/projects/jsoncpp/, I downloaded the v0.5.0, the compressed package is about 104K.
Method One: Use the Lib file generated by Jsoncpp
unzip the download aboveJsoncppFiles, find Jsoncpp.sln in the Jsoncpp-src-0.5.0/makefiles/vs71 directory, compile with the VS2008 version, and generate the static link library by default. Referenced in the project, you only need to include the header file under Include/json and the generated. lib file.
How to include Lib files: in a. cpp file#pragma comment (lib. Json_vc71_libmt.lib "), in the Project attribute linker input Additional dependencies write the Lib file name (under release forJson_vc71_libmt.lib,debug forJson_vc71_libmtd.lib)
Note:the LIB Project compilation options for Jsoncpp are consistent with the compilation options in the VS project. If the Lib File Engineering compilation option is MT (or MTD), the VS project should also select Mt(or MTD), or there will be a compilation error problem, Debug and release under the Lib file generated under different names, be careful not to look at the wrong, as a file to use (I made this mistake).
Method Two: Use the. cpp in the Jsoncpp package and
. h file
unzip the download aboveJsoncppfile, putjsoncpp-src-0.5.0 files are copied to the project directory,will beJsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\jsonand theJsoncpp-src-0.5.0\jsoncpp-src-0.5.0\src\lib_jsonthe files in the catalog are included in the VS Project, in the properties of VS project under GeneralAdditional Include Directoriesinclude header file directory. \jsoncpp-src-0.5.0\include. In the CPP file that is usedcontains JSON header files, such as:#include "Json/json.h". Will beJson_reader.cpp,Json_value.cpp andJson_writer.cpp The precompiled header property of three files is set toNot
Using precompiled Headers,Otherwise, there will be an error compiling.
Jsoncpp use of the detailed
Jsoncpp mainly contains three types of Class:value, Reader, Writer. All objects in Jsoncpp, class names in namespace Json, contain json.h.
Json::value can only handle strings of ANSI type, and if the C + + program is Unicode encoded, it is best to add a Adapt class to fit.
TestJsoncppCode.cpp:Defines the entry point for the console application. 02.//03. #include "stdafx.h" 05. . #include "Include/json/json.h" #include <fstream>. #include <string> 10. One. #pragma comment (lib, "Lib_json.lib") 12.using namespace std; #ifdef _DEBUG. #define NEW debug_new. #endif 17. 19.//the one and only Application object 20. 22.void writejsondata (const char* filename) 23. {Json::reader Reader; Json::value Root; Json::value is a very important type that can represent any type. such as int, string, object, array ... 26.27. Std::ifstream is; Is.open (filename, std::ios::binary); . if (Reader.parse (is, root)) 30. {Json::value arrayobj; Build Object 32. Json::value New_item, new_item1; new_item["date"] = "2011-11-11"; new_item1["Time"] = "11:11:11"; Arrayobj.append (New_item); Insert array member 36. Arrayobj.append (NEW_ITEM1); Insert Array member 37. int file_size = root["Files"].size (); for (int i = 0; i < file_size; ++i) 39. root["Files"][i]["exifs"] = arrayobj; Insert the original JSON in 40. std::string out = root.tostyledstring (); 41.//Output unformatted JSON string 42. Json::fastwriter writer; std::string Strwrite = writer.write (root); Std::ofstream OFS; Ofs.open ("Test_write.json"); OFS << Strwrite; Ofs.close (); 48.} 49. Is.close (); 51.} 52. 53.int readjsonfromfile (const char* filename) 54. {Json::reader reader;//parsing Json with Json::reader 56. Json::value Root; Json::value is a very important type that can represent any type. such as int, string, object, array ... 57.58. Std::ifstream is; Is.open (filename, std::ios::binary); if (Reader.parse (is, root, false)) 61. {std::string code; if (!root["Files"].isnull ())//Access node, access a object value by name, create a NULL member if it does not EXist. Code = root["Uploadid"].asstring (); 65.66. Code = root.get ("Uploadid", "null"). asstring ();//Access node, Return the member named key if it exist, defaultvalue otherwise. 67.68. int file_size = root["Files"].size (); Get the array number of "files" 69. for (int i = 0; i < file_size; ++i)//traversal array 70. {Json::value val_image = root["Files"][i]["Images"]; image_size int = val_image.size (); for (int j = 0; j < image_size; ++j) 74. {std::string type = val_image[j]["type"].asstring (); std::string url = val_image[j]["url"].asstring (); A. printf ("Type:%s, url:%s \ n", Type.c_str (), Url.c_str ()); 78.} 79. } 80. } 81. Is.close (); 82.83. return 0; 84.} 85. 86.int Main (int argc, tchar* argv[], tchar* envp[]) 87. {nRetCode int = 0; 89.90. 91.//1. Parsing JSON 92 from a string. Const CHAr* str = "{\" uploadid\ ": \" up000000\ ", \" code\ ": 100,\" msg\ ": \" \ ", \" files\ ": \" \ "}"; 93.94. Json::reader Reader; Json::value Root; if (Reader.parse (str, root))//Reader parses the JSON string into root,root will contain all the child elements in JSON 97. {98. printf ("--------------read JSON---------------\ n" from a string); std::string upload_id = root["Uploadid"].asstring (); Access node, upload_id = "UP000000" 100. int code = root["Code"].asint (); Access node, code = 100 101. 102. printf ("upload_id:%s\ncode:%d \ n", Upload_id.c_str (), code); 103.} 104. 105.//2. Parsing JSON 106 from a file. String Stemppath = "Test_json.json"; 107.108. printf ("--------------read JSON---------------\ n" from a file); 109. Readjsonfromfile (Stemppath.c_str ()); 110.111. 112.//3. Write JSON 113 to the file. Writejsondata (Stemppath.c_str ()); 114.115. System ("pause"); 116.117. return nretcode; 118.}
C + + parsing json--jsoncpp