Jsoncpp Library of C + + libraries

Source: Internet
Author: User

Official website Https://github.com/open-source-parsers/jsoncpp.

I am using the form of source code, from the official website to download the source code, and then copy the corresponding. h and. cpp files to your project directory. Compile and use. It is also very simple to use, mainly in the form of arrays

Use. Here's an example of a piece of code copied from your own project:

JSON data:

{
"MSG": "",
"Status": "1",
"Message": {
"Yuyue": [
{
"id": "1776",
"Tid": "2",
"userid": "77878",
"Yuyuetime": "2015-05-07",
"CATID": "5",
"Create_time": "1430130036",
"Type": "1",
"Contact": "13501621744",
"SJD": "16:00-17:00",
"From": "1",
"title": "Parent-child relationship",
"username": "Test Account",
"Padcode": null,
"Iptvcode": "021000008003"
},
{
"id": "1539",
"Tid": "2",
"userid": "33990",
"Yuyuetime": "2015-04-16",
"CATID": "746",
"Create_time": "1428458444",
"Type": "0",
"Contact": "50691881/13917894758",
"SJD": "09:00-10:00",
"from": "0",
"title": null,
"username": "Sfsfs",
"Padcode": null,
"Iptvcode": "021000008004"
}
]
}
}

Parsing code:

void Loaddocjson ()
{
Parsing JSON with Json::reader
Json::reader Reader;
Std::ifstream file;
File.Open ("Doctorylist.json");

Json::value Root;
Json::value item;
Reader.parse (File,root);

int size = root["message" ["Yuyue"].size ();

Doctoryinfo Dcinfo;

for (int i=0; i<size; ++i)
{
QString id=qstring::fromstdstring (root["message" ["Yuyue"][i]["id"].asstring ());
strcpy (Dcinfo.id,id.tostdstring (). C_STR ());

QString userid=qstring::fromstdstring (root["message" ["Yuyue"][i]["userid"].asstring ());
strcpy (Dcinfo.userid,userid.tostdstring (). C_STR ());

QString username=qstring::fromstdstring (root["message" ["Yuyue"][i]["username"].asstring ());
strcpy (Dcinfo.username,username.tostdstring (). C_STR ());

QString titleclass=qstring::fromstdstring (root["message" ["Yuyue"][i]["title"].asstring ());
strcpy (Dcinfo.titleclass,titleclass.tostdstring (). C_STR ());


QString yuyuetime=qstring::fromstdstring (root["message" ["Yuyue"][i]["Yuyuetime"].asstring ());
strcpy (Dcinfo.yuyuetime,yuyuetime.tostdstring (). C_STR ());

QString create_time=qstring::fromstdstring (root["message" ["Yuyue"][i]["Create_time"].asstring ());
strcpy (Dcinfo.create_time,create_time.tostdstring (). C_STR ());

QString iptvcode=qstring::fromstdstring (root["message" ["Yuyue"][i]["Iptvcode"].asstring ());
strcpy (Dcinfo.iptvcode,iptvcode.tostdstring (). C_STR ());

Map_docinfo.insert (Dcinfo.id,dcinfo);

Qdebug () <<dcinfo.userid<< "xxxxxxxx";
}

}

It is also worth pointing out that, for Chinese, JSON network transmission is generally Unicode encoding, parsing often garbled, in fact, the Jsoncpp library has done a perfect solution, but need to modify the next source.

Open the file json_reader.cpp, locate the CodePointToUTF8 function, and add the following code:

else if (CP <= 0X7FF)
{
Result.resize (2);
RESULT[1] = static_cast<char> (0x80 | (0x3f & CP));
Result[0] = static_cast<char> (0xC0 | (0x1f & (CP >> 6)));
}
Byliu Add-begin
else if ((CP >= 0x4e00 && CP <= 0x9fa5) | | (CP >= 0xf900 && cp<= 0xfa2d))
{
wchar_t src[2]={0};
Char dest[5]={0};
Src[0]=static_cast<wchar_t> (CP);
std::string Curlocale=setlocale (lc_all,null);
SetLocale (Lc_all, "CHS");
wcstombs_s (null,dest,5,src,2);
Result=dest;
SetLocale (Lc_all,curlocale.c_str ());
}
Beliu Add-end

Of course, the jsoncpp can be compiled into Lib library to invoke. I am using JSON in QT, the QT comes with the library did not study, but personally feel this C + + library good use. Also about xml,c++ there is a library, as if called markup, also

Very easy to use, personal feeling than the QT comes with the library is much more useful. Make an introduction in another article.


Jsoncpp Library of C + + libraries

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.