一、jsoncpp介绍
jsoncpp是一个开源C++库,提供对JSON字符串序列化/反序列化的功能。
开源地址:GitHub - open-source-parsers/jsoncpp: A C++ library for interacting with JSON.
文档地址:http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html
二、jsoncpp的使用
jsoncpp主要包含三种类型的C++类 - value、reader、writer。value表示json对象和数组。reader用于反序列化json字符串。writer用于序列化json字符串。简单使用示例:
示例1:生成json
Json::Value jsonRoot; //定义根节点
Json::Value jsonItem; //定义一个子对象
jsonItem["item1"] = "one"; //添加数据
jsonItem["item2"] = 2;
jsonRoot.append(jsonItem);
jsonItem.clear(); //清除jsonItemjsonItem["item1.0"] = 1.0;
jsonItem["item2.0"] = 2.0;
jsonRoot["item"] = jsonItem;
std::string strJson = jsonRoot.toStyledString(); //json结果
示例2:读取json
Json::Reader reader;
string json_document = "{\"age\" : 123, \"name\" : \"weng\"}";
if (!reader.parse(json_document, json_object))
{
cout << "error" << endl;
return 0;
}
else
{
cout <<"age:" <<json_object["age"] << " name" << json_object["name"] << endl;
}
示例3:空数组
Json::Value root;
root["FaceItemObjects"].resize(0);
std::string strJson = root.toStyledString();
序列化复杂json字符串
//写json对象数组
Json::Value root;
Json::Value itemFaceArray;
Json::value itemFace;
root["errorCode"] = 0;
root["faces"].resize(0);
//face1
itemFace["x"]=1;
itemFace["y"]=1;
itemFace["width"]=100;
itemFace["height"]=100;
itemFace["score"]=95;
itemFaceArray.append(itemFace);
//face2
itemFace.clear();
itemFace["x"]=200;
itemFace["y"]=200;
itemFace["width"]=100;
itemFace["height"]=100;
itemFace["score"]=98;
itemFaceArray.append(itemFace);
root["faces"] = itemFaceArray;
std::string strJson = root.toStyledString();
//读json对象数组
Json::value itemFace;
int nFaceCount = root["faces"].size();
int n = 0;
for(n=0; n<nFaceCount; n++)
{
itemFace = root["faces"][n];
int nScore = itemFace["score"].asInt();
}
示例5:从文件中读取JSON。
#include <string>
#include <json/json.h>
#include <iostream>
#include <fstream>
//从文件中读取JSON
void readFileJson()
{
Json::Reader reader;
Json::Value root;
//从文件中读取,保证当前文件有test.json文件
ifstream in("test.json", ios::binary);
if( !in.is_open() )
{
cout << "Error opening file\n";
return;
}
if(reader.parse(in,root))
{
//读取根节点信息
string name = root["name"].asString();
int age = root["age"].asInt();
}
else
{
cout << "parse error\n" << endl;
}
in.close();
}
示例6:写JSON至文件中。
//写JSON至文件中
void writeFileJson()
{
//根节点
Json::Value root;
//根节点属性
root["name"] = Json::Value("Mike");
root["age"] = Json::Value(21);
//输出到文件
ofstream os;
os.open("test.json");
os << sw.write(root);
os.close();
}
三、注意事项
1、jsoncpp不支持int64位的。
解决方法1:需要0.6.0rc2版本。 不要采用0.5.0版本,因为它没有64位int。
解决方法2:0.5.0版本基础上修改代码支持64位int。具体参考json-cpp / Discussion / Open Discussion: Support for int64/uint64 numbers
解决方法3:asUInt换成asDouble
Json::Reader reader;
Json::Value root;
if (reader.parse(str, root))
{
//获取里面内容
OutputDebugString(_T("STRING TO JSON \n"));
//std::string str1 = root["messageType"].asString();
long long tmstamp = ((long long)(root["sendTime"].asUInt()))/1000;
WCHAR* wstr = NULL;
TimestampToLocalTime(&wstr,tmstamp);
}
结果发现第8行会出错,查了下错误原因, 原来SendTime是一个一毫秒为单位的时间戳,其值为1403575350411,这个值的大小远远超出了 unsigned int 或者 int的最大值,只能用INT64来表示, 但是看看Json::Value里面的函数只有asInt, asUint,没有取64位整数的函数,那怎么办呢?里面虽然没有64位的但是有一个asDouble,duoble的指数为11位,能表示的范围比Int64还大,所以上面的asUInt换成asDouble就可以了。
解决方法4:使用高版本(比如1.8.4),在使用jsoncpp库的工程属性预定义中增加JSON_HAS_INT64
2、获取json中不存在名称的值,并且作为asCString()拷贝时,程序会core。如strncpy(a, root["test"].asCString(), sizeof(a));
3、解决jsoncpp中文输出为unicode格式的"\u"、VS读取utf8格式中文输出乱码问题。jsoncpp写文件中文,不进行streamWritebuilder的设置,写入的中文会变成unicode形式(比如:\u0375 \u0376)。
高版本jsoncpp(比如:1.8.4)不需要修改代码。。低版本jsoncp,需要修改改代码参考:c++ jsoncpp使用toStyledString生成字符串中文乱码解决方案_tostylestring_wgxh05的博客-CSDN博客
4、jsoncpp中文输出不带\u格式(\u表示unicode编码;不带\u就是使用utf-8编码),使用streamWritebuilder。
//写文件
#include <codecvt>
#include<windows.h>
#include<iostream>
#include<string>
#include<json/json.h>
#include<fstream>
Json::Value root;
//中文转换成utf-8
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
std::string utf8_string = convert.to_bytes(L"王五");
root["name"] = utf8_string;
root["age"] = 20;
//关键在于对builder的属性设置
Json::StreamWriterBuilder builder;
static Json::Value def = []() {
Json::Value def;
Json::StreamWriterBuilder::setDefaults(&def);
def["emitUTF8"] = true;
return def;
}();
builder.settings_ = def;//Config emitUTF8
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
std::fstream fs;
fs.open("save.json", std::ios::out);
writer->write(root, &fs);
fs.close();
//写utf-8返回字符串
std::string JsonToString(const Json::Value & root)
{
static Json::Value def = []() {
Json::Value def;
Json::StreamWriterBuilder::setDefaults(&def);
def["emitUTF8"] = true;
return def;
}();
std::ostringstream stream;
Json::StreamWriterBuilder stream_builder;
stream_builder.settings_ = def;//Config emitUTF8
std::unique_ptr<Json::StreamWriter> writer(stream_builder.newStreamWriter());
writer->write(root, &stream);
return stream.str();
}
//读文件
#include <codecvt>
#include<windows.h>
#include<iostream>
#include<string>
#include<json/json.h>
#include<fstream>
//读入文件时将文件编码设置为UTF-8
std::ifstream ifs("file.json", std::ios::in | std::ios::binary);
ifs.imbue(std::locale(std::locale(), new std::codecvt_utf8<wchar_t>));
Json::Reader reader;
Json::Value root;
if(reader.parse(ifs,root))
{
//读取根节点信息
string name = root["name"].asString();
int age = root["age"].asInt();
}
else
{
cout << "parse error\n" << endl;
}
ifs.close();
5、fatal error C1083: 无法打开编译器生成的文件:“../../build/vs71/release/lib_json\json_value.asm”: No such file or directory
解决方法:修改生成静态库文件的工程的属性:路径为:菜单---项目--属性---配置属性---c/c++---输出文件---汇编程序输出:无列表
6、异常捕获
bool bReadJsonSuccess = false;
try
{
Json::Reader reader;
string json_document = "{\"age\" : 123, \"name\" : \"weng\"}";
if (!reader.parse(json_document, json_object))
{
cout << "error" << endl;
bReadJsonSuccess = false;
}
else
{
cout <<"age:" <<json_object["age"] << " name" << json_object["name"] << endl;
bReadJsonSuccess = true;
}
}
catch(...)
{
bReadJsonSuccess = false;
}
7、json字符串的格式化及非格式化转换。
转为格式化字符串,里面加了很多空格及换行符
string strJson1 = root.toStyledString();
转为未格式化字符串,无多余空格及换行符
Json::FastWriter writer;
string strJson1 = writer.write(root);
8、遍历Json::Value
1)利用getMemberNames方法获取Json::Value的键列表,本质是字符串向量。
typedef std::vector<std::string> Json::Value::Members;
2)再根据 key 值寻找Value,然后递归调用。
void objectValue(Json::Value &value)
{
Json::Value::Members mem = value.getMemberNames();
for (auto iter = mem.begin(); iter != mem.end(); iter++) {
printf("%s : ", (*iter).c_str()); // 打印键名
typeSwitch(value[*iter]);
}
}
void typeSwitch(Json::Value &value)
{
switch (value.type())
{
case Json::objectValue :
objectValue(value);
break;
case Json::arrayValue :
{
auto count = value.size();
for (unsigned int i = 0; i < count; i++)
typeSwitch(value[i]);
}
break;
case Json::stringValue :
printf("%s\n", value.asString().c_str());
break;
case Json::realValue :
printf("%lf\n", value.asDouble());
break;
case Json::uintValue :
printf("%u\n", value.asUInt());
break;
case Json::intValue :
printf("%d\n", value.asInt());
break;
case Json::booleanValue :
printf("%d\n", value.asBool());
break;
default :
break;
}
}
9、jsoncpp 中关于浮点数的控制问题。
解决方法1:浮点数使用字符串替换。
解决方法2:使用precision控制精度。比如:
writerBuilder.settings_[“precision”] = 16;
该选项表示浮点数能存储的位数的最大个数,包括整数位,默认是17
#incllude <string>
#include <iostream>
using namespace std;
int main(int argc, const char *argv[])
{
Json::Value rootInput;
Json::CharReaderBuilder readerBuilder;
char szInputContent[256] = {0};
sprintf(szInputContent, "%s", "{\"name\":\"john\",\"age\":3.15}");
unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
if(!rjsonReader->parse(szInputContent, &rootInput)) {
cout << "parseJson err. " << endl;
return -1;
}
string name = rootInput["name"].asString();
double age = rootInput["age"].asDouble();
cout << "name:"<<name << " age:" <<age << endl;
string jsonStr;
Json::Value root;
Json::StreamWriterBuilder writerBuilder;
std::ostringstream os;
int c = 4;
string str_get;
double get = precision(age, c, str_get);
root["Name"] = name;
root["Age"] = get;
writerBuilder.settings_["precision"] = 16;
//writerBuilder.settings_["emitUTF8"] = true;
std::unique_ptr<Json::StreamWriter> jsonWriter(writerBuilder.newStreamWriter());
jsonWriter->write(root, &os);
jsonStr = os.str();
cout << (char *)jsonStr.c_str() << endl;
return 0;
}