1  jsoncpp的api简要说明

1,解析(json字符串转为对象)

std::string strDataJson;

Json::Reader JReader;

Json::Value JObject;

if (!JReader.parse(strDataJson, JObject))

{

cerr << "parse json error." << endl;

return bSuccess;

}

2,读取

std::string strMsg = JRec["msg"].asString();

int nRetCode = JRec["ret"]..asInt();

Json::Value JList = JRec["data"]["list"];

int nSize = JList.size();

获取错误信息: JReader.getFormatedErrorMessages()

3,增加或修改

JRoot["stringdata"] = Json::Value("msg");

JRoot["intdata"] = Json::Value(10);

4,删除

JValue.removeMember("toberemove");

5,对象转为字符串

//输出无格式json字符串

Json::FastWriter fast_writer;

strJRecList = fast_writer.write(JRoot);

//格式化之后的json,有回车换行符

std::string strOut = JRoot.toStyledString();

#include "json/json.h"
const string fileName = "json.txt"; int main(int argc, char *argv[])
{
string line;
std::ifstream in(fileName.c_str());
if(!in)
return ; std::getline(in, line); Json::Reader reader;
Json::Value root; if(reader.parse(line, root))
cout << "suc" << endl;
else
cout << "fail" << endl; cout << root["status"].asInt() << endl;
cout << root["msg"].asString() << endl;
cout << root["forbidReason"].asString() << endl;
    Json::Value root, ipPort;
string host;
unsigned int port = ; if(addrs.size() == )
root["hosts"].append(ipPort);
else
{
for(size_t i = ; i < addrs.size(); i++)
{
if(getIpAndPort(addrs[i], host, port))
{
ipPort["ip"] = host;
ipPort["port"] = port;
}
root["hosts"].append(ipPort);
}
}

http://blog.csdn.net/u014489596/article/details/44920557

son是一种数据交换格式,比较适合编写和阅读。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对象中,方便解析,后面都是如此

  1. std::string json = "{\"id\" : 123, \"name\" : \"wu\"}";
  2. Json::Reader reader;
  3. Json::Value root;
  4. std::string name;
  5. int id = 0;
  6. if (reader.parse(json, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素
  7. {
  8. name = root["name"].asString();
  9. id = root["id"].asInt();
  10. }

2.再看看数组的:

[ { "id" : 1, "name" : "wu"},  {"id":2, "name" : "tan"} ]

  1. std::string json = "[ {\"id\" : 1, \"name\" : \"wu\"}, {\"id\" : 2, \"name\" : \"tan\"} ]";
  2. Json::Reader reader;
  3. Json::Value root;
  4. std::string name;
  5. int id = 0;
  6. std::map<int, std::string> mapJson;
  7. if (reader.parse(json, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素
  8. {
  9. for (int i = 0; i < root.size(); ++i)
  10. {
  11. name = root[i]["name"].asString();
  12. id = root[i]["id"].asInt();
  13. mapJson[id] = name;
  14. }
  15. }

3.如果是这样的数组:

{

“id” : [1, 2],

"name" : ["wu", "tan"]

}

  1. std::string json = "{\"id\" : [1, 2], \"name\" : [\"wu\", \"tan\"] } ";
  2. Json::Reader reader;
  3. Json::Value root;
  4. std::string name;
  5. int id = 0;
  6. if (reader.parse(json, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素
  7. {
  8. for (int i = 0; i < root["id"].size(); ++i)
  9. {
  10. id = root["id"][i].asInt();
  11. }
  12. for (int i = 0; i < root["name"].size(); ++i)
  13. {
  14. name = root["name"][i].asString();
  15. }
  16. }

这种情况其实和上一种是类似的。

4.看看多重嵌套的情况,为了简便,我们嵌套两层:

{

"id" : 1,

"data" : {

"name" : "wu",

“age” : 26

}

}

  1. std::string json = "{\"id\" : 1, \"data\" : { \"name\" : \"wu\",  \"age\" : 26 } }";
  2. Json::Reader reader;
  3. Json::Value root;
  4. std::string name;
  5. int id = 0;
  6. int age = 0;
  7. if (reader.parse(json, root))  // reader将Json字符串解析到root,root将包含Json里所有子元素
  8. {
  9. id = root["id"].asInt();
  10. name = root["data"]["name"].asString();
  11. age = root["data"]["age"].asInt();
  12. }

其实这种情况和第一种的类似,只是通过root["key"]取到的还是键值对,继续通过key取值即可。

基本上再复杂的数据格式也是上面几种情况的组合而已。

json对象的生成:

1.生成上面第一种情况的json格式:

  1. Json::Value root;
  2. root["id"] = 123;
  3. root["name"] = "wu";
  4. std::string json = root.toStyledString();

我们会将生成的json对象序列化到string对象中去,后面也是如此。

2.生成上面第二种情况的json:

  1. Json::Value root;
  2. for (int i = 0; i < 2; ++i)
  3. {
  4. root[i]["id"] = i + 1;
  5. if (0 == i)
  6. {
  7. root[i]["name"] = "wu";
  8. }
  9. else
  10. {
  11. root[i]["name"] = "tan";
  12. }
  13. }
  14. std::string json = root.toStyledString();

还可以这样生成:

  1. Json::Value root;
  2. Json::Value item;
  3. for (int i = 0; i < 2; ++i)
  4. {
  5. item["id"] = i + 1;
  6. if (0 == i)
  7. {
  8. item["name"] = "wu";
  9. }
  10. else
  11. {
  12. item["name"] = "tan";
  13. }
  14. root.append(item);
  15. }
  16. std::string json = root.toStyledString();

3.生成上面第三种情况的json:

  1. Json::Value root;
  2. for (int i = 0; i < 2; ++i)
  3. {
  4. root["id"].append(i);
  5. if (0 == i)
  6. {
  7. root["name"].append("wu");
  8. }
  9. else
  10. {
  11. root["name"].append("tan");
  12. }
  13. }
  14. std::string json = root.toStyledString();

4.生成上面第四种情况的json:

  1. Json::Value root;
  2. root["id"] = 1;
  3. root["data"]["name"] = "wu";
  4. root["data"]["age"] = 26;
  5. std::string json = root.toStyledString();

其实解析和生成json是互逆的,只要明白这几种情况,其他的无非是这几种情况的各种组合,原理是一样的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. 彻底解决rman恢复碰到ora-01152错
  2. 判断 JS 中对象的类型
  3. CEF 相关资料
  4. Microsoft 2013 新技术学习笔记 一
  5. 13Spring_AOP编程(AspectJ)_后置通知
  6. mysql is marked as crashed and should be repaired错误
  7. css应对已有class和特殊class的冲突
  8. Swift-3-字符串和字符
  9. Java Hashtable类
  10. IE6 png兼容问题
  11. GDI+创建Graphics对象的2种方式
  12. python基础教程
  13. C#代码生成工具:文本模板初体验 使用T4批量修改实体框架(Entity Framework)的类名
  14. 微信小程序之初探(常见语法 VS vue)常见问题(点击不生效,数据绑定)
  15. WEB安全之垃圾信息防御措施
  16. Codeforces 590D Top Secret Task
  17. maven打包含有多个main程序的jar包及运行方式
  18. 2--linux命令--查看磁盘空间
  19. 使用prometheus+ grafana+nginx-module-vts 模块监控openresty
  20. POJ 1679 The Unique MST (次小生成树kruskal算法)

热门文章

  1. python爬虫---实现项目(二) 分析Ajax请求抓取数据
  2. Perl语言入门: 斜线不是元字符,所以在不作为分隔符时不需要加上反斜线。
  3. Gym-101615C-Fear Factoring(数论)
  4. [CF] 37 E. Trial for Chief
  5. Windows环境下安装 mysql-8.0.11-winx64 遇到的问题解决办法
  6. Oracle的五种约束
  7. python基础003
  8. LeetCode(171) Excel Sheet Column Number
  9. 【HIHOCODER 1043】题目1 : 完全背包
  10. win10 命令行无法直接ping的问题解决方法