从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

// TestJsoncppCode.cpp : Defines the entry point for the console application.
// #include "stdafx.h" #include "include/json/json.h"
#include <fstream>
#include <string> #pragma comment(lib,"lib_json.lib")
using namespace std; #ifdef _DEBUG
#define new DEBUG_NEW
#endif // The one and only application object void WriteJsonData(const char* filename)
{
Json::Reader reader;
Json::Value root; // Json::Value是一种非常重要的类型。能够代表随意类型。 如int, string, object, array... std::ifstream is;
is.open(filename, std::ios::binary);
if (reader.parse(is, root))
{
Json::Value arrayObj; // 构建对象
Json::Value new_item, new_item1;
new_item["date"] = "2011-11-11";
new_item1["time"] = "11:11:11";
arrayObj.append(new_item); // 插入数组成员
arrayObj.append(new_item1); // 插入数组成员
int file_size = root["files"].size();
for (int i = 0; i < file_size; ++i)
root["files"][i]["exifs"] = arrayObj; // 插入原json中
std::string out = root.toStyledString();
// 输出无格式json字符串
Json::FastWriter writer;
std::string strWrite = writer.write(root);
std::ofstream ofs;
ofs.open("test_write.json");
ofs << strWrite;
ofs.close();
} is.close();
} int ReadJsonFromFile(const char* filename)
{
Json::Reader reader;// 解析json用Json::Reader
Json::Value root; // Json::Value是一种非常重要的类型,能够代表随意类型。如int, string, object, array... std::ifstream is;
is.open(filename, std::ios::binary);
if (reader.parse(is, root, false))
{
std::string code;
if (!root["files"].isNull()) // 訪问节点。Access an object value by name, create a null member if it does not exist.
code = root["uploadid"].asString(); code = root.get("uploadid", "null").asString();// 訪问节点,Return the member named key if it exist, defaultValue otherwise. int file_size = root["files"].size(); // 得到"files"的数组个数
for (int i = 0; i < file_size; ++i) // 遍历数组
{
Json::Value val_image = root["files"][i]["images"];
int image_size = val_image.size();
for (int j = 0; j < image_size; ++j)
{
std::string type = val_image[j]["type"].asString();
std::string url = val_image[j]["url"].asString();
printf("type : %s, url : %s \n", type.c_str(), url.c_str());
}
}
}
is.close(); return 0;
} int main(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0; //1.从字符串解析json
const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}"; Json::Reader reader;
Json::Value root;
if (reader.parse(str, root)) // reader将Json字符串解析到root,root将包括Json里全部子元素
{
printf("--------------从字符串读取JSON---------------\n");
std::string upload_id = root["uploadid"].asString(); // 訪问节点。upload_id = "UP000000"
int code = root["code"].asInt(); // 訪问节点,code = 100 printf("upload_id : %s\ncode : %d \n", upload_id.c_str(), code);
} //2.从文件解析json
string sTempPath = "test_json.json"; printf("--------------从文件读取JSON---------------\n");
ReadJsonFromFile(sTempPath.c_str()); //3.向文件写入json
WriteJsonData(sTempPath.c_str()); system("pause"); return nRetCode;
}

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. LR接口测试---基于http协议之get/post

    get请求代码: //=====================get interface======================== 以http状态码方式: //获取返回的HTTP状态码判断请求 ...

  2. mongo 3.4分片集群系列之四:搭建分片集群--哈希分片 + 安全 + 区域

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  3. 生产环境4.3.5jboss内存调优

    1.查看jboss的监控工具 http://XXX/jmx-console/htmladaptor 2.查看jvm的监控工具 jdk\bin jvisualvm.exe jmc.exe 3.查看jbo ...

  4. R语言曲线拟合函数(绘图)

    曲线拟合:(线性回归方法:lm) 1.x排序 2.求线性回归方程并赋予一个新变量     z=lm(y~x+I(x^2)+...) 3.plot(x,y)    #做y对x的散点图 4.lines(x ...

  5. AMH V4.5 – 基于AMH4.2的第三方开发版

    AMH V4.5[基于AMH4.2第三方开发版]重新部署了一次安装脚本,修改一系列BUG,已完美支持CENTOS7,树莓派,Fedora,Aliyun,Amazon,debian,Ubuntu,Ras ...

  6. Bootstrap Datatable 简单的基本配置

    $(document).ready(function() {     $('#example').dataTable({ "sScrollX": "100%", ...

  7. Objective-C中copy 、retain以及ARC中新加入的strong、weak关键字的含义

    copy: 创建一个引用计数为1的对象,然后释放旧的对象 retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的引用计数为 1 Copy其实是建立了一个相同的对象,而retain不是: ...

  8. Java集合(一)--Comparable和Comparator

    Comparable: 是集合内部的方法实现的排序,只有一个方法 public interface Comparable<T> { public int compareTo(T o); } ...

  9. ZOJ - 3983 - Crusaders Quest(思维 + 暴力)

    题意: 给出一个字符串,长度为9,包含三种各三个字母"a","g","o",如果一次消除连续三个一样的分数+1,消完自动向左补齐 其中可以消 ...

  10. trie字典树模板浅析

    什么是trie? 百度百科 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的 ...