"Jsoncpp" Introduction and Demo

Source: Internet
Author: User
Tags string to json

I. INTRODUCTION of JSON

Json

A lightweight data interchange format, easy to read, write, parse, all called Javsscript objectnotation.

JSON is made up of two basic structures

A collection of ① name/value pairs that can be understood as objects

A combination of ② values that can be understood as an array

Example

stringstrtemp ="{\ "name\": \ "cuihao\","         "\ "age\":"; stringStrroot ="{\ "key_string\": \ "value_string\","         "\ "Key_number\":"                       "\ "Key_boolean\": false,"                   "\ "Key_double\": 12.345,"                  "\ "key_object\": Json_temp,"                "\ "key_array\": [\ "array_string1\", \ "Array_string2\", 12345]}";

Second, Jsoncpp

1. Jsoncpp Introduction

Jsoncpp is one of the commonly used parsing libraries for C + + parsing JSON strings. The commonly used classes are:

A) Json::value can represent all types, such as Int,string,object,array, whose supported types can refer to the values in Json:valuetype.

b) json::reader the Json file stream or string to Json::value, and the main function has parse.

c) Json::writer, in contrast to Json::reader, transforms Json::value into a string stream, noting its two subclasses: Json::fastwriter and Json::stylewriter, Outputs unformatted JSON and formatted JSON, respectively.

D) Json::value::members is primarily used to parse Json arrays in STL style. People who have seen the source code know that the members are typedefvector.

Using Jsoncpp in VC

After downloading the source code, enter D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\makefiles\vs71 similar directory to compile with VS Open. sln file.

Attention:

Can be compiled directly under debug

Release: Will Lib_json Project properties: Configuration Properties-c/c++--output file: assembly Output Select "No list", should be no lists.

When using Jsoncpp, add

Header file directory: D:\SourceSoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include

Library Directory:

Debug:d:\sourcesoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\debug\lib_json

#pragma commebt (lib, "Json_vc71_libmtd.lib") corresponds to/MDD

Release:d:\sourcesoftware\jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\build\vs71\release\lib_json

#pragma commebt (lib, "Json_vc71_libmt.lib") corresponds to/MT

Three, the code

#include"stdafx.h"#include<iostream>#include<string>#include<json\json.h>using namespacestd;int_tmain (intARGC, _tchar*argv[]) {     stringStrstudent ="{\ "name\": \ "cuishihao\", \ "age\":, \ "major\": \ "Cs\"}"; //char* strstudent = "{\" name\ ": \" cuishihao\ ", \" age\ ":" major\ ": \" Cs\ "}";Json::reader Reader;    Json::value Value; BOOLBRet =reader.parse (strstudent, value); if(false==BRet) {Cerr<< Endl <<"Read value error \ n"; return-1; } cout<< value["name"].asstring () <<Endl; cout<< value[" Age"].asint () <<Endl; cout<< value["Major"].asstring () <<Endl; cout<<Endl;    Json::value json_temp; json_temp["name"] = Json::value ("Cuihao"); json_temp[" Age"] = Json::value ( -);    Json::value Root; root["key_string"] = Json::value ("value_string"); root["Key_number"] = Json::value (12345); root["Key_boolean"] = Json::value (true); root["key_double"] = Json::value (12.345); root["Key_object"] =json_temp; root["Key_array"].append ("array_string1"); root["Key_array"].append ("array_string2"); root["Key_array"].append (12345); Json::valuetype type=Root.type (); Json::value Arrvalue= root["Key_array"];  for(Json::value::uint i =0; I < arrvalue.size (); ++i) {if(Arrvalue[i].isstring ()) cout<<arrvalue[i].asstring (); Else if(Arrvalue[i].isint ()) cout<<Arrvalue[i].asint (); cout<<Endl; } cout<< Endl <<"----------------------------------- "<<Endl; stringstrtemp ="{\ "name\": \ "cuihao\","         "\ "age\":"; stringStrroot ="{\ "key_string\": \ "value_string\","         "\ "Key_number\":"                       "\ "Key_boolean\": false,"                   "\ "Key_double\": 12.345,"                  "\ "key_object\": Json_temp,"                "\ "key_array\": [\ "array_string1\", \ "Array_string2\", 12345]}";    Json::styledwriter writer; cout<< Endl << writer.write (Root) <<Endl; cout<< Endl <<"----------------------------"<<Endl; Json::value::members Members=Root.getmembernames ();  for(Json::value::members::const_iterator iter =Members.begin (); ITER!=Members.end (); ++ITER) {        stringStrName = *ITER; if(Root[strname].isint ()) cout<<Root[strname].asint (); Else if(Root[strname].isstring ()) cout<<root[strname].asstring (); Else if(Root[strname].isdouble ()) cout<<root[strname].asdouble (); Else if(Root[strname].isbool ()) cout<<Root[strname].asbool (); Else if(Root[strname].isarray ()) { for(Json::value::arrayindex i =0; I < root[strname].size (); ++i) {if(Root[strname][i].isint ()) cout<<Root[strname][i].asint (); Else if(Root[strname][i].isstring ()) cout<<root[strname][i].asstring (); Elsecout<<"others"; cout<<Endl; }        }         Else if(Root[strname].isobject ()) {json::value::members MBS=Root[strname].getmembernames ();  for(Json::value::members::const_iterator iter2 =Mbs.begin (); Iter2!=Mbs.end (); ++iter2) {                stringstrName2 = *Iter2; if(Root[strname][strname2].isint ()) cout<<Root[strname][strname2].asint (); Else if(Root[strname][strname2].isstring ()) cout<<root[strname][strname2].asstring (); Elsecout<<"others"; cout<<Endl; } // for}//else if        Elsecout<<"others"; cout<<Endl; }    return 0;}

Execution results

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.