C + + parsing json--jsoncpp

Source: Internet
Author: User


JSON (JavaScript Object Notation) is a lightweight data interchange format, similar to XML, this article mainly on the use of Jsoncpp parsing JSON VS2008 the way to do a record.
Jsoncpp is a cross-platform open Source Library: http://sourceforge.net/projects/jsoncpp/, I downloaded v0.5.0, compressed package about 104K.


Method One: Use the Lib file generated by jsoncpp

Unzip the downloaded Jsoncpp file above, find the 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: #pragma comment (lib) in the. cpp file. Json_vc71_libmt.lib "), in the Project attribute linker Input Additional dependencies write Lib file name (under release for json_vc71_ Libmt.lib,debug for Json_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), vs Engineering also choose MT (or MTD), or there will be a compilation error problem, the name of the Lib file generated under Debug and release is different, 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 downloaded Jsoncpp file above, copy the jsoncpp-src-0.5.0 file to the project directory and jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\ The files in the JSON and Jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\src\lib_json directories are included in the VS Project, under the properties of the VS Project, general additional include Directories contains the header file directory. \jsoncpp-src-0.5.0\include. Include the JSON header file in the CPP file you are using, such as: #include "json/json.h". Set the precompiled header property of the Json_reader.cpp, Json_value.cpp, and json_writer.cpp three files to the not 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.


Here is an example of a code search from the Internet:
1. Parsing json from a string

Const char* str = "{\" uploadid\ ": \" up000000\ ", \" code\ ": 100,\" msg\ ": \" \ ", \" files\ ": \" \ "}";

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 the JSON
{
std::string upload_id = root["Uploadid"].asstring (); Access node, upload_id = "UP000000"
int code = root["Code"].asint (); Access node, code = 100
}

2. Parsing json from a file

int readjsonfromfile (const char* filename)
{
Json::reader reader;//parsing json with Json::reader
Json::value Root; Json::value is a very important type that can represent any type. such as int, string, object, array

Std::ifstream is;
Is.open (filename, std::ios::binary);
if (Reader.parse (is, root, FALSE))
{
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 ();

Code = root.get ("Uploadid", "null"). asstring ();//Access node, Return the member named key if it exist, defaultvalue otherwise.

int file_size = root["Files"].size (); Get the number of "files" array
for (int i = 0; i < file_size; ++i)//traversal array
{
Json::value val_image = root["Files"][i]["Images"];
int image_size = Val_image.size ();
for (int j = 0; j < image_size; ++j)
{
std::string type = val_image[j]["type"].asstring ();
std::string url = val_image[j]["url"].asstring ();
printf ("Type:%s, url:%s \ n", Type.c_str (), Url.c_str ());
}
}
}
Is.close ();

return 0;
}

3. Inserting JSON into the file

void Writejsondata (const char* filename)
{
Json::reader Reader;
Json::value Root; Json::value is a very important type that can represent any type. such as int, string, object, array

Std::ifstream is;
Is.open (filename, std::ios::binary);
if (Reader.parse (is, root))
{
Json::value arrayobj; Building objects
Json::value New_item, new_item1;
new_item["date"] = "2011-11-11";
new_item1["Time"] = "11:11:11";
Arrayobj.append (New_item); Inserting array members
Arrayobj.append (NEW_ITEM1); Inserting array members
int file_size = root["Files"].size ();
for (int i = 0; i < file_size; ++i)
root["Files"][i]["exifs"] = arrayobj; Insert in original JSON
std::string out = root.tostyledstring ();
Output unformatted JSON string
Json::fastwriter writer;
std::string Strwrite = writer.write (root);
Std::ofstream OFS;
Ofs.open ("Test_write.json");
OFS << Strwrite;
Ofs.close ();
}

Is.close ();
}

C + + parsing json--jsoncpp

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.