Jsoncpp的安装与使用
在实际项目中,很多的启动文件,配置文件与数据传输格式都是使用json来进行的,好处不便多说。
1.安装
本人操作系统为ubuntu 16.04,直接运行下列语句就安装完毕。
将 /libjson_linux-gcc-5.4.0_libmt.* 放在 /lib/ 目录下。
sudo apt-get install libjsoncpp-dev
2.使用
**(1)先上要解析的json格式文件
其文件名为:json_text.txt
{
"face":
[
{
"attribute":
{
"age":27,
"gender":"Male",
"lefteye_opendegree":43,
"mouth_opendegree":2,
"pose":
{
"raise":-21,
"tilting":-2,
"turn":4
},
"righteye_opendegree":47
},
"face_id":"83e3edac3d5d49579089d3ee043d41ec",
"position":
{
"center":{"x":179,"y":50},
"eye_left":{"x":206,"y":78},
"eye_right":{"x":248,"y":78},
"height":94,
"width":94
},
"tag":""
}
],
"devices": [u'000_ce21',u'0000_ce22'],
"img_height":500,
"img_id":"030-3aff983ea4ea5a35f89ae6",
"img_width":350,
"res_code":"0000",
"url":"http://e.hiphotos.baidu.com/baike/pic/item/9345d688d43f879428d347b3d81b0ef41bd53a7a.jpg"
}
(2)解析
“devices”: [u’000_ce21’,u’0000_ce22’],此种格式在json 是认识的,因此我们先要对其进行处理,转换为:”devices”: [“000_ce21”,”0000_ce22”]。具体的解析代码如下:
命名为 jsoncpp_parse_test.cpp
#include <iostream>
#include <fstream>
#include "json/json.h"
using namespace std;
using namespace Json;
string & replace_all(string& str,const string& old_value,const string& new_value)
{
while(true)
{
string::size_type pos(0);
if((pos=str.find(old_value))!=string::npos)
str.replace(pos,old_value.length(),new_value);
else
break;
}
return str;
}
int main()
{
Json::Value root;
Json::Reader reader;
string inputsStr,onelineStr;
std::ifstream ifs;
ifs.open("json_text.txt");
while (getline(ifs,onelineStr))
{
inputsStr+=onelineStr;
}
//cout<<inputsStr<<endl<<endl;
inputsStr = replace_all(inputsStr,"u'","\"");
inputsStr = replace_all(inputsStr,"'","\"");
//cout<<inputsStr<<endl<<endl;
if(!reader.parse(inputsStr, root)){
// fail to parse
cout << "img_width:\t"<<"error parse"<<endl;
}
else
{
// success
cout<<root["face"][0U]["face_id"];
//string faceId = root["face"][0]["face_id"].asString();
int imgWidth = (int)root["img_width"].asInt();
int imgHeight = root["img_height"].asInt();
string resCode = root["res_code"].asString();
string url = root["url"].asString();
//cout << "face_id:\t" << faceId << endl;
cout << "img_width:\t" << imgWidth << endl;
cout << "img_height:\t" << imgHeight << endl;
cout << "res_code:\t" << resCode << endl;
cout << "url:\t\t" << url << endl;
unsigned int dev_size = root["devices"].size();
for (unsigned int i = 0; i < dev_size; ++i)
{
cout<<"i:"<<i<<root["devices"][i]<<endl;
}
}
return 0;
}
基本思路:
将json中的内容加载到string 变量inputsStr中,在用reader.parse进行匹配是否符合json格式。在解析中数组的下标是unsigned int 类型,整数数在编译器中默认的是int类型,可以在其后面加U,比如0则为0U,否则会编译通过,运行segment error。
(3)编译脚本
#!/bin/sh
g++ jsoncpp_parse_test.cpp -o test_cpp /lib/libjson_linux-gcc-5.4.0_libmt.so
(4)运行结果
(5)有时 键值是不确定的,那如何解析呢?
json文件格式:
{"hunan":["cs","zz","xt"],"jiangxi":["nc","xy"]}
解析代码
#include <iostream>
#include <fstream>
#include "json/json.h"
using namespace std;
using namespace Json;
int main()
{
Value root;
Reader reader;
string inputsStr,onelineStr;
ifstream ifs;
ifs.open("json_not_known_key.txt");
while (getline(ifs,onelineStr))
{
inputsStr+=onelineStr;
}
if(!reader.parse(inputsStr,root))
{
// fail to parse
cout<<" "<<"fail to parse,file not json type"<<endl;
}
else
{
Json::Value::Members member = root.getMemberNames();
for(Json::Value::Members::iterator iter = member.begin(); iter != member.end(); ++iter)
{
string area = *iter;
unsigned int city_size = root[*iter].size();
cout<<"city "<<city_size<<endl;
for (unsigned int iUid = 0; iUid < city_size; ++iUid)
{
string city = root[*iter][iUid].asString();
cout<<"area: "<<area<<" city: "<< city<<endl;
}
}
}
}
结果: