使用jsoncpp开源库,进行盲解析string格式json串
jsoncpp版本1.9.5,注意低版本可能不支持代码中的(isObject())方法
具体实现代码如下:
#pragma once
#include <json/json.h>
#include <string>
/*
*@bref 递归解析json value
*/
void recurseAnalyzeJson(const Json::Value& root_value);
/*
*@bref 解析json字符串
*@param strData 输入string格式的字符串
*/
void analyzeJson(const std::string& strData);
#include "json_parse.h"
#include <iostream>
#include <stdlib.h>
void recurseAnalyzeJson(const Json::Value& root_value)
{
if (!root_value.isObject())
return;
int iLen = root_value.size();
Json::Value::Members members;
for (auto iterVal = root_value.begin(); iterVal != root_value.end(); iterVal++)
{
Json::Value::Members members = root_value.getMemberNames();
for (Json::Value::Members::iterator iter = members.begin(); iter != members.end(); iter++)
{
if (iterVal.key() == *iter)
{
if (!root_value[(*iter)].isObject())
printf("key is %s: ", std::string((*iter)).c_str());
if (root_value[(*iter)].isArray())
{
if (root_value[(*iter)].size() > 0)
{
printf("\n");
for (size_t n = 0; n < root_value[(*iter)].size(); n++)
{
if (root_value[(*iter)][n].isInt())
{
printf("value type is int: %s ", std::to_string(root_value[(*iter)][n].asInt()).c_str());
}
else if (root_value[(*iter)][n].isDouble())
{
printf("value type is double: %s ", std::to_string(root_value[(*iter)][n].asDouble()).c_str());
}
else if (root_value[(*iter)][n].isInt64())
{
printf("value type is int64: %s ", std::to_string(root_value[(*iter)][n].asInt64()).c_str());
}
else if (root_value[(*iter)][n].isString())
{
printf("value type is string: %s ", root_value[(*iter)][n].asString().c_str());
}
else if (root_value[(*iter)][n].isBool())
{
printf("value type is bool: %s ", root_value[(*iter)][n].asBool() ? "true" : "false");
}
else
{
printf("\n");
}
printf("\n");
}
}
else
{
printf("\n");
}
}
else if (root_value[(*iter)].isBool())
{
printf("value type is bool: %s", root_value[(*iter)].asBool() ? "true" : "false");
}
else if (root_value[(*iter)].isDouble())
{
printf("value type is double: %s: ", std::to_string(root_value[(*iter)].asDouble()).c_str());
}
else if (root_value[(*iter)].isInt64())
{
printf("value type is int64: %s: ", std::to_string(root_value[(*iter)].asInt64()).c_str());
}
else if (root_value[(*iter)].isInt())
{
printf("value type is int: %s: ", std::to_string(root_value[(*iter)].asInt()).c_str());
}
else if (root_value[(*iter)].isString())
{
printf("value type is string: %s: ", root_value[(*iter)].asString().c_str());
}
else if (root_value[(*iter)].isNull())
{
printf("null \n");
}
else if (root_value[(*iter)].isObject())
{
recurseAnalyzeJson(root_value[(*iter)]);
}
else
{
printf("\n");
}
printf("\n");
}
}
}
return;
}
void analyzeJson(const std::string& strData)
{
Json::Reader readJson;
Json::Value root_value;
if (readJson.parse(strData, root_value) && root_value.isObject())
{
recurseAnalyzeJson(root_value);
}
}
测试用例:
#include "json_parse.h"
#include <iostream>
#include <stdlib.h>
int main()
{
Json::Value root;
Json::Value item;
Json::Value array_;
root["first"] = std::string("this is a test example !");
item["a"] = std::string("aaa");
item["b"] = std::string("bbb");
item["c"] = std::string("ccc");
item["d"] = std::string("ddd");
item["e"] = true;
item["f"] = 64;
item["g"] = 33.33;
root["second"] = item;
root["array"] = Json::Value::nullSingleton();
for (int i = 0; i < 10; i++) {
if (i < 5)
array_.insert(i, i * 3);
else
array_.insert(i, i * 5.1);
}
root["array"] = array_;
std::string input_data = root.toStyledString();
analyzeJson(input_data);
return 0;
}