jsoncp是一个操作json类型的c++库,可用于json串的解析与构造。其优点是语法简单,但是效率不如rapidjson。
源码下载链接:open-source-parsers/jsoncpp
Documention: JsonCpp documentation
其中最简单的方法是执行项目根目录中的python脚本,构建头文件和源文件。
在安装Python环境的控制台中进入jsoncpp项目根目录,
执行命令:
python amalgamate.py
3. 将生成的dist目录拷贝到自己的项目中,其中包源文件jsoncpp.cpp和头文件json.h、json-forwards.h。
使用时只需要包含
基本步骤:
bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isInt64() const;
bool isUInt() const;
bool isUInt64() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;
jsoncpp中的类型转化函数如下:
const char* asCString() const; ///< Embedded zeroes could cause you trouble!
#if JSONCPP_USING_SECURE_MEMORY
unsigned getCStringLength() const; // Allows you to understand the length of
// the CString
#endif
JSONCPP_STRING asString() const; ///< Embedded zeroes are possible.
/** Get raw char* of string-value.
* \return false if !string. (Seg-fault if str or end are NULL.)
*/
bool getString(char const** begin, char const** end) const;
#ifdef JSON_USE_CPPTL
CppTL::ConstString asConstString() const;
#endif
Int asInt() const;
UInt asUInt() const;
#if defined(JSON_HAS_INT64)
Int64 asInt64() const;
UInt64 asUInt64() const;
#endif // if defined(JSON_HAS_INT64)
LargestInt asLargestInt() const;
LargestUInt asLargestUInt() const;
float asFloat() const;
double asDouble() const;
bool asBool() const;
下面是具体的解析伪代码示例(注释行为使用的deprecated的Json::Read类):
void parseJsonString(string & str)
{
//Json::Reader json_reader;
Json::CharReaderBuilder builer;
builder["collectComments"] = false;
Json::Value json_value;
std::string errs;
const char * p_str = str.c_str();
Json::CharReader* reader = builder.newCharReader();
//if(!json_reader.parse(p_str, p_str + str.size(), json_value, false) || (!json_value.isObject()))
if(!reader->parse(p_str, p_str + str.size(), &json_value, &errs) || (!json_value.isObject()))
{
cout << "parse " << str << "fail" << endl;
delete reader;
return ;
}
Json::Value var = json_value["key"];
if(var.isNull())
{
...
}else if(var.isObject)
{
cout << "var is a object" << endl;
//continue to parse the object
...
}else if(var.isString())
{
cout << var.asString() << endl;
}else if(var.isInt()){
...
}
...
delete reader;
}
如果输入的json内容位于文件的话,可以使用Json::CharReadBuilder来解析[需要将如下的std::cin替换成fstream变量]:
// Here, using a specialized Builder, we discard comments and
// record errors as we parse.
Json::CharReaderBuilder rbuilder;
rbuilder["collectComments"] = false;
std::string errs;
Json::Value root;
bool ok = Json::parseFromStream(rbuilder, std::cin, &root, &errs);
构造示例代码如下:
void constructJsonString(string & str)
{
Json::Value root;
Json::FastWriter writer;
Json::Value element;
element["name"] = "wang";
root["key"] = element;
str = writer.write(root);
cout << str << endl;
}
在新的版本中FastWriter类被声明为deprecated,推荐使用StreamWriterBuilder,示例代码如下:
void constructJsonString1(string & str)
{
Json::Value root;
Json::StreamWriterBuilder writerbuilder;
Json::Value element;
element["name"] = "wang";
root["key"] = element;
str = Json::writeString(writerbuilder, root);
cout << str << endl;
}