rapidjson 实现未知json数据解析,拿到每一个key和value
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
利用rapidjson实现未知json数据的解析,拿到每一个key和value,用vs2010实现。
引用头文件
#include "rapidjson/document.h"
#include <string>
using namespace std;
using namespace rapidjson;
程序段
string strJsonTest = "{\"item_1\":\"value_1\",\"item_2\":\"value_2\",\"item_3\":\"value_3\",\"item_4\":\"value_4\",\"item_arr\":[\"arr_vaule_1\",\"arr_vaule_2\"]}";
Document docTest;
docTest.Parse<0>(strJsonTest.c_str());
if (!docTest.HasParseError())
{
for (rapidjson::Value::ConstMemberIterator itr = docTest.MemberBegin(); itr != docTest.MemberEnd(); itr++)
{
Value jKey;
Value jValue;
Document::AllocatorType allocator;
jKey.CopyFrom(itr->name, allocator);
jValue.CopyFrom(itr->value,allocator);
if (jKey.IsString())
{
string name = jKey.GetString();
printf("\r\nname: %s\r\n",name.c_str());
}
}
}
利用jsoncpp实现未知json数据的解析,拿到每一个key和value,用vs2010实现。
Json::Value valRoot;
Json::Reader reader;
if (!reader.parse(strJson, valRoot))
{
return JSON_PARSE_ERR;
}
Json::Value::Members members;
members = valRoot.getMemberNames();
for (Json::Value::Members::iterator iterMember = members.begin(); iterMember != members.end(); iterMember++)
{
std::string strKey = *iterMember;
if (valRoot[strKey.c_str()].isString())
{
std::string strVal = valRoot[strKey.c_str()].asString();
std::cout<<strKey.c_str()<<":"<<strVal.c_str()<<std::endl;
}
else if (valRoot[strKey.c_str()].isInt())
{
int iVal = valRoot[strKey.c_str()].asInt();
std::cout<<strKey.c_str()<<":"<<iVal<<std::endl;
}
else if (valRoot[strKey.c_str()].isDouble())
{
double dVal = valRoot[strKey.c_str()].asDouble();
std::cout<<strKey.c_str()<<":"<<dVal<<std::endl;
}
else
{
std::string strVal = valRoot[strKey.c_str()].toStyledString();
std::cout<<strKey.c_str()<<":"<<strVal.c_str()<<std::endl;
}
}
提供两个函数例子实现:
jsoncpp:
int PrintJsonString(const std::string& strJson)
{
Json::Value valRoot;
Json::Reader reader;
if (!reader.parse(strJson, valRoot))
{
return -1;
}
Json::Value::Members members;
members = valRoot.getMemberNames();
for (Json::Value::Members::iterator iterMember = members.begin(); iterMember != members.end(); iterMember++)
{
std::string strKey = *iterMember;
if (valRoot[strKey.c_str()].isString())
{
std::string strVal = valRoot[strKey.c_str()].asString();
std::cout<<strKey.c_str()<<":"<<strVal.c_str()<<std::endl;
}
else if (valRoot[strKey.c_str()].isInt())
{
int iVal = valRoot[strKey.c_str()].asInt();
std::cout<<strKey.c_str()<<":"<<iVal<<std::endl;
}
else if (valRoot[strKey.c_str()].isDouble())
{
double dVal = valRoot[strKey.c_str()].asDouble();
std::cout<<strKey.c_str()<<":"<<dVal<<std::endl;
}
else
{
std::string strVal = valRoot[strKey.c_str()].toStyledString();
std::cout<<strKey.c_str()<<":"<<strVal.c_str()<<std::endl;
}
}
return 0;
}
rapidjson:
std::string JsonToString(rapidjson::Value& valRoot)
{
rapidjson::StringBuffer strBuf;
strBuf.Clear();
rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
valRoot.Accept(writer);
std::string strJson = strBuf.GetString();
return strJson;
}
int PrintJsonString(const std::string& strJson)
{
int iStart = strJson.find("{");
int iEnd = strJson.rfind("}");
if ((iStart < 0) || (iEnd < 1) || (iStart > iEnd))
{
return -1;
}
std::string strTemp = strJson.substr(iStart, (iEnd - iStart + 1));
rapidjson::Document doc;
doc.Parse(strTemp.c_str());
if (doc.HasParseError())
{
return -1;
}
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
for (rapidjson::Value::ConstMemberIterator iter = doc.MemberBegin(); iter != doc.MemberEnd(); iter++)
{
rapidjson::Value jKey;
rapidjson::Value jValue;
jKey.CopyFrom(iter->name, allocator);
jValue.CopyFrom(iter->value, allocator);
if (jKey.IsString())
{
std::string strKey = jKey.GetString();
if (jValue.IsString())
{
std::string strVal = jValue.GetString();
std::cout<<strKey.c_str()<<":"<<strVal.c_str()<<std::endl;
}
else if (jValue.IsInt())
{
int iVal = jValue.GetInt();
std::cout<<strKey.c_str()<<":"<<iVal<<std::endl;
}
else if (jValue.IsDouble())
{
double dVal = jValue.GetDouble();
std::cout<<strKey.c_str()<<":"<<dVal<<std::endl;
}
else
{
std::string strVal = JsonToString(jValue);
std::cout<<strKey.c_str()<<":"<<strVal.c_str()<<std::endl;
}
}
}
return 0;
}
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献1条内容
所有评论(0)