从github下载jsoncpp-master

在执行\jsoncpp-master\makefiles\msvc2010文件夹下jsoncpp.sln

会有3个项目

执行lib_json项目生成lib_json.lib。这个静态库就是我们想要的。

这里要注意的是:“执行lib_json项眼下要设置一下c/c++-》代码生成-》执行库以便生成不一样的lib文件”

假设lib要用于MTd环境下,则设置为MTd;假设lib要用于MDd环境下,则设置为MDd。



jsoncpp的使用:http://www.cppblog.com/wanghaiguang/archive/2013/12/26/205020.html

  1. // TestJsoncppCode.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include "include/json/json.h"
  7. #include <fstream>
  8. #include <string>
  9.  
  10. #pragma comment(lib,"lib_json.lib")
  11. using namespace std;
  12.  
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #endif
  16.  
  17. // The one and only application object
  18.  
  19. void WriteJsonData(const char* filename)
  20. {
  21. Json::Reader reader;
  22. Json::Value root; // Json::Value是一种非常重要的类型。能够代表随意类型。
  23.  
  24. int, string, object, array...
  25.  
  26. std::ifstream is;
  27. is.open(filename, std::ios::binary);
  28. if (reader.parse(is, root))
  29. {
  30. Json::Value arrayObj; // 构建对象
  31. Json::Value new_item, new_item1;
  32. new_item["date"] = "2011-11-11";
  33. new_item1["time"] = "11:11:11";
  34. arrayObj.append(new_item); // 插入数组成员
  35. arrayObj.append(new_item1); // 插入数组成员
  36. int file_size = root["files"].size();
  37. for (int i = 0; i < file_size; ++i)
  38. root["files"][i]["exifs"] = arrayObj; // 插入原json中
  39. std::string out = root.toStyledString();
  40. // 输出无格式json字符串
  41. Json::FastWriter writer;
  42. std::string strWrite = writer.write(root);
  43. std::ofstream ofs;
  44. ofs.open("test_write.json");
  45. ofs << strWrite;
  46. ofs.close();
  47. }
  48.  
  49. is.close();
  50. }
  51.  
  52. int ReadJsonFromFile(const char* filename)
  53. {
  54. Json::Reader reader;// 解析json用Json::Reader
  55. Json::Value root; // Json::Value是一种非常重要的类型,能够代表随意类型。如int, string, object, array...
  56.  
  57. std::ifstream is;
  58. is.open(filename, std::ios::binary);
  59. if (reader.parse(is, root, false))
  60. {
  61. std::string code;
  62. if (!root["files"].isNull()) // 訪问节点。Access an object value by name, create a null member if it does not exist.
  63. code = root["uploadid"].asString();
  64.  
  65. code = root.get("uploadid", "null").asString();// 訪问节点,Return the member named key if it exist, defaultValue otherwise.
  66.  
  67. int file_size = root["files"].size(); // 得到"files"的数组个数
  68. for (int i = 0; i < file_size; ++i) // 遍历数组
  69. {
  70. Json::Value val_image = root["files"][i]["images"];
  71. int image_size = val_image.size();
  72. for (int j = 0; j < image_size; ++j)
  73. {
  74. std::string type = val_image[j]["type"].asString();
  75. std::string url = val_image[j]["url"].asString();
  76. printf("type : %s, url : %s \n", type.c_str(), url.c_str());
  77. }
  78. }
  79. }
  80. is.close();
  81.  
  82. return 0;
  83. }
  84.  
  85. int main(int argc, TCHAR* argv[], TCHAR* envp[])
  86. {
  87. int nRetCode = 0;
  88.  
  89. //1.从字符串解析json
  90. const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";
  91.  
  92. Json::Reader reader;
  93. Json::Value root;
  94. if (reader.parse(str, root)) // reader将Json字符串解析到root,root将包括Json里全部子元素
  95. {
  96. printf("--------------从字符串读取JSON---------------\n");
  97. std::string upload_id = root["uploadid"].asString(); // 訪问节点。upload_id = "UP000000"
  98. int code = root["code"].asInt(); // 訪问节点,code = 100
  99.  
  100. printf("upload_id : %s\ncode : %d \n", upload_id.c_str(), code);
  101. }
  102.  
  103. //2.从文件解析json
  104. string sTempPath = "test_json.json";
  105.  
  106. printf("--------------从文件读取JSON---------------\n");
  107. ReadJsonFromFile(sTempPath.c_str());
  108.  
  109. //3.向文件写入json
  110. WriteJsonData(sTempPath.c_str());
  111.  
  112. system("pause");
  113.  
  114. return nRetCode;
  115. }

jsoncpp的生成和使用的更多相关文章

  1. 使用jsoncpp解析生成json

    在此站点下载jsoncpp(https://sourceforge.net/projects/jsoncpp/这个站点的版本较旧) 在电脑上安装Python,运行amalgamate.py,生成的di ...

  2. jsoncpp 不能处理long类型数据

    jsoncpp,是一个c++的解析和生成json的开源工具.假设你的c++程序须要解析或生成json,它会使这个过程变得非常easy! 可是,今天在用jsoncpp进行生成json的时候报了错误,非常 ...

  3. 【独立开发人员er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势

    学习cocos2d-x和cocos creator的圈子:cocos2d-x:436689827    cocos creator:124727696 本篇文章主要内容:jsoncpp的使用,Coco ...

  4. Oracle 11g数据库详解(2)

    FAILED_LOGIN_ATTEMPTS 用于指定连续登陆失败的最大次数 达到最大次数后,用户会被锁定,登陆时提示ORA-28000 UNLIMITED为不限制 精确无误差 是 实时 PASSWOR ...

  5. jsoncpp 生成 json 字符串

    Json::Value root; Json::Value arrayObj; Json::Value item; for (int i=0; i<10; i++) { item["k ...

  6. C++处理Json串——jsoncpp库

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml类似,本文主要对VS2008中使用Jsoncpp解析json的方法做一下记录.Jsoncpp是个跨 ...

  7. json 对c++类的序列化(自动生成代码)

    [动机] 之前写网络协议的时候,使用的是google protobuf,protobuf不但在性能和扩展性上有很好的优势,protoc自动生成c++类代码的工具,这点确实给程序员带来了很多便利. 做后 ...

  8. Jsoncpp Compiler、Programming

    catalog . C++ jsoncpp简介 . Jsoncpp的下载与编译 . Linux Jsoncpp的SDK编译 & 简单实例 . Windows Jsoncpp的SDK编译 &am ...

  9. JsonCpp的简单使用方法

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programming Langu ...

随机推荐

  1. mysql复制过滤参数说明

    参考文档: http://www.ywnds.com/?p=6945 https://stackoverflow.com/questions/23191160/whats-the-difference ...

  2. vuex的各个细节理解(因人而异)

    应用级的状态集中放在store中: 改变状态的方式是提交mutations,这是个同步的事物: 异步逻辑应该封装在action中. const vuex_store = new Vuex.store( ...

  3. jquery.ajax之beforeSend方法使用介绍

    常见的一种效果,在用ajax请求时,没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据.这个就可以用beforeSend方法来实现. 下载demo:a ...

  4. 运用反射时报错java.lang.NoSuchMethodException,以解决,记录一下

    问题:想调用service类中的私有方法时, Method target=clz.getMethod("say", String.class);用Class的getMethod报错 ...

  5. codeforces_302D

    D. Yaroslav and Time time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. CSS绝对定位模拟固定定位

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 获取最新ADT下载地址的方法

    最近网络不给力,谷歌上不去,想下个最新的ADT插件也难,于是寻找方法,最后找到一个不错的方法,问题解决过程如下(别嫌我啰嗦啊). 网上有人分享过下载ADT插件的页面地址:install-adt.htm ...

  8. EXP-00083: 调用 EXFSYS.DBMS_EXPFIL_DEPASEXP.schema_info_exp 时出现前一问题

    select owner,object_name,object_type,status from dba_objects where object_name = 'LT_EXPORT_PKG'; 如果 ...

  9. 【2018百度之星资格赛】 A 问卷调查 - 位运算&动规

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6344 参考博客:在此感谢http://www.cnblogs.com/LQLlulu/p/941923 ...

  10. 每日命令:(1)ls

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...