json是一种数据交换格式,比较适合编写和阅读。jsoncpp是采用c++语言编写的用来处理json格式的第三包。直接来说明改如何使用它,本文是基于windows下的。
在github上下载jsoncpp的源代码包:https://github.com/open-source-parsers/jsoncpp。解压后用vs打开/makefiles/vs71/jsoncpp.sln项目,选择lib_json项目编译来生成lib文件,为了方便,debug和release都需要生成。
创建一个win32的空项目,将生成的lib文件包含,附加包含目录添加源代码中的include文件夹。后面简单说下比较常用的几种json处理方法。
解析json对象:
1.首先看看最简单的一种json格式,只有键-值的一重嵌套:
{
“id” : 123,
"name" : "wu"
}
我们直接将上面的数据初始化到到string对象中,方便解析,后面都是如此
std::string json = "{\"id\" : 123, \"name\" : \"wu\"}";
Json::Reader reader;
Json::Value root;
std::string name;
int id = 0;
if (reader.parse(json, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
name = root["name"].asString();
id = root["id"].asInt();
}
2.再看看数组的:
[ { "id" : 1, "name" : "wu"}, {"id":2, "name" : "tan"} ]
std::string json = "[ {\"id\" : 1, \"name\" : \"wu\"}, {\"id\" : 2, \"name\" : \"tan\"} ]";
Json::Reader reader;
Json::Value root;
std::string name;
int id = 0;
std::map<int, std::string> mapJson;
if (reader.parse(json, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
for (int i = 0; i < root.size(); ++i)
{
name = root[i]["name"].asString();
id = root[i]["id"].asInt();
mapJson[id] = name;
}
}
3.如果是这样的数组:
{
“id” : [1, 2],
"name" : ["wu", "tan"]
}
std::string json = "{\"id\" : [1, 2], \"name\" : [\"wu\", \"tan\"] } ";
Json::Reader reader;
Json::Value root;
std::string name;
int id = 0;
if (reader.parse(json, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
for (int i = 0; i < root["id"].size(); ++i)
{
id = root["id"][i].asInt();
}
for (int i = 0; i < root["name"].size(); ++i)
{
name = root["name"][i].asString();
}
}
这种情况其实和上一种是类似的。
4.看看多重嵌套的情况,为了简便,我们嵌套两层:
{
"id" : 1,
"data" : {
"name" : "wu",
“age” : 26
}
}
std::string json = "{\"id\" : 1, \"data\" : { \"name\" : \"wu\", \"age\" : 26 } }";
Json::Reader reader;
Json::Value root;
std::string name;
int id = 0;
int age = 0;
if (reader.parse(json, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
{
id = root["id"].asInt();
name = root["data"]["name"].asString();
age = root["data"]["age"].asInt();
}
其实这种情况和第一种的类似,只是通过root["key"]取到的还是键值对,继续通过key取值即可。
基本上再复杂的数据格式也是上面几种情况的组合而已。
json对象的生成:
1.生成上面第一种情况的json格式:
Json::Value root;
root["id"] = 123;
root["name"] = "wu";
std::string json = root.toStyledString();
我们会将生成的json对象序列化到string对象中去,后面也是如此。
2.生成上面第二种情况的json:
Json::Value root;
for (int i = 0; i < 2; ++i)
{
root[i]["id"] = i + 1;
if (0 == i)
{
root[i]["name"] = "wu";
}
else
{
root[i]["name"] = "tan";
}
}
std::string json = root.toStyledString();
还可以这样生成:
Json::Value root;
Json::Value item;
for (int i = 0; i < 2; ++i)
{
item["id"] = i + 1;
if (0 == i)
{
item["name"] = "wu";
}
else
{
item["name"] = "tan";
}
root.append(item);
}
std::string json = root.toStyledString();
3.生成上面第三种情况的json:
Json::Value root;
for (int i = 0; i < 2; ++i)
{
root["id"].append(i);
if (0 == i)
{
root["name"].append("wu");
}
else
{
root["name"].append("tan");
}
}
std::string json = root.toStyledString();
4.生成上面第四种情况的json:
Json::Value root;
root["id"] = 1;
root["data"]["name"] = "wu";
root["data"]["age"] = 26;
std::string json = root.toStyledString();
其实解析和生成json是互逆的,只要明白这几种情况,其他的无非是这几种情况的各种组合,原理是一样的。