最近开始使用 jsoncpp,以前一直在使用cJSON,但是使用cJSON的时候经常会忘记free掉json的内存,结果造成了内存泄露,程序跑着跑着就崩溃了。所以最近把json转移到了jsoncpp上。

jsoncpp的代码源文件 百度网盘 : http://pan.baidu.com/s/1ntqQhIT

下面是jsoncpp源代码中的main.cpp实例,大家感受一下。

jsoncpp代码包解压后的目录结构如下:

jsoncpp/
├─ include
│   └─ json
│       ├── autolink.h
│       ├── config.h
│       ├── features.h
│       ├── forwards.h
│       ├── json.h
│       ├── reader.h
│       ├── value.h
│       └── writer.h
├─ makefile
└─ src
    ├─ json
    │   ├── json_batchallocator.h
    │   ├── json_internalarray.inl
    │   ├── json_internalmap.inl
    │   ├── json_reader.cpp
    │   ├── json_value.cpp
    │   ├── json_valueiterator.inl
    │   ├── json_writer.cpp
    │   └── sconscript
    └── main.cpp

其中jsoncpp/src/main.cpp就是我们的测试程序代码。

在jsoncpp目录下直接运行命令make即可进行编译(linux环境下)。

 #include <string>
#include <json/json.h> void readJson();
void writeJson(); int main(int argc, char** argv) {
readJson();
writeJson();
return ;
} void readJson() {
using namespace std;
std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"; Json::Reader reader;
Json::Value value; if (reader.parse(strValue, value))
{
std::string out = value["name"].asString();
std::cout << out << std::endl;
const Json::Value arrayObj = value["array"];
for (unsigned int i = ; i < arrayObj.size(); i++)
{
if (!arrayObj[i].isMember("cpp"))
continue;
out = arrayObj[i]["cpp"].asString();
std::cout << out;
if (i != (arrayObj.size() - ))
std::cout << std::endl;
}
}
} void writeJson() {
using namespace std; Json::Value root;
Json::Value arrayObj;
Json::Value item; item["cpp"] = "jsoncpp";
item["java"] = "jsoninjava";
item["php"] = "support";
arrayObj.append(item); root["name"] = "json";
root["array"] = arrayObj; root.toStyledString();
std::string out = root.toStyledString();
std::cout << out << std::endl;
}

下面是运行的结果

 json
jsoncpp
{
"array" : [
{
"cpp" : "jsoncpp",
"java" : "jsoninjava",
"php" : "support"
}
],
"name" : "json"
}

作者:风波

mail : fengbohello@qq.com

jsoncpp代码实例的更多相关文章

  1. MATLAB的PLOT函数线型设置及横坐标为字符串的代码实例

    2.横坐标为字符串的代码实例 cell={‘PLS’,’SVM’,’RF’,’NNET’,’NB’,’PLR’,’C5.0′,’PDA’,’KNN’,’GLM’,’BCT’};%分类方法yData=[ ...

  2. web service上传参数代码实例

    web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...

  3. (转)combogrid的代码实例

    EasyUI中combogrid的代码实例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  4. PHP生成迅雷、快车、旋风等软件的下载链接代码实例

    PHP生成迅雷.快车.旋风等软件的下载链接代码实例 <?php function Download() { $urlodd=explode('//',$_POST["url" ...

  5. Scala 深入浅出实战经典 第45讲: scala中context bounds代码实例

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  6. 关于JAVA中事件分发和监听机制实现的代码实例-绝对原创实用

    http://blog.csdn.net/5iasp/article/details/37054171 文章标题:关于JAVA中事件分发和监听机制实现的代码实例 文章地址: http://blog.c ...

  7. input文本框实现宽度自适应代码实例

    代码实例如下: <!DOCTYPE html> <html><head><meta charset="utf-8"><meta ...

  8. jQuery实现的鼠标滑过切换图片代码实例

    jQuery实现的鼠标滑过切换图片代码实例:有时候网页需要这样的简单效果,那就是当鼠标滑过默认图片的时候,能够实现图片的切换,可能在实际应用中,往往没有这么简单,不过大家可以自行扩展一下,下面简单介绍 ...

  9. input文本框实现宽度自适应代码实例,input文本框

    本章节介绍一下如何让一个文本框的宽度能够随着文本框中的内容的宽度增长而增长,也就是能够实现宽度自适应效果. 代码实例如下: <!DOCTYPE html> <html> < ...

随机推荐

  1. java面试总结-(hibernate ibatis struts2 spring)

    说说Hibernate对象的三种状态 Hibernate对象有三种状态,分别是:临时态(Transient). 持久态(Persistent).游离态(Detached). 临时状态:是指从对象通过n ...

  2. 在SQLServer处理中的一些问题及解决方法 NEWSEQUENTIALID()

    一.DBLINK性能问题select * from dbsource.dbname.dbo.table where guid in (select guid from tablechangelog w ...

  3. Java for LeetCode 168 Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  4. Java for LeetCode 077 Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. 【python】在python中调用mysql

    资料:http://www.runoob.com/python/python-mysql.html 例子: 假设有数据路HTMLHASH, 其中有表htmlhash, 表中包括两个varchar(10 ...

  6. ubuntu 图形界面查看隐藏文件

    在 Linux 下以 . 开头的文件或文件夹为隐藏文件,在图形界面(nautilus)下可用 CTRL + H 显示隐藏文件,终端下者可以用 ls -a 显示所有文件.

  7. C/C++函数参数读取顺序

    #include <iostream> #include <stdio.h> using namespace std; void B(int a, int b){ cout & ...

  8. 编译预处理命令--define和ifdef的使用

    这里将对常用的预处理命令进行学习. 一.宏定义  ·defined 格式:`defined     宏名      数值 或者 `define      宏名 注意:后面没有‘;‘,和单片机不一样: ...

  9. 关于Java异常和错误的几个问题

    1.Java中什么是Exception? 异常是Java传达给你的系统和程序错误的方式. 在java中,异常功能是通过实现比如Throwable,Exception,RuntimeException之 ...

  10. 添加Java文档注释

    一.在Eclipse中add javadoc comment的快捷键为: 快捷键为:ALT + SHIFT +J 二.Window-->Preferences-->General--> ...