使用jsoncpp进行字符串、数字、布尔值和数组的封装与解析。

1)下载jsoncpp的代码库 百度网盘地址 :http://pan.baidu.com/s/1ntqQhIT

2)解压缩文件 jsoncpp.rar

unzip jsoncpp.rar

3)修改jsoncpp/src/main.cpp文件

vim src/main.cpp
 #include <string>
#include <json/json.h>
#include "stdio.h" int ReadJson(const std::string &);
std::string writeJson(); int main(int argc, char** argv)
{
using namespace std; std::string strMsg; cout<<"--------------------------------"<<endl;
strMsg = writeJson();
cout<< "json write : " << endl << strMsg << endl;
cout<<"--------------------------------"<<endl;
cout<< "json read :" << endl;
ReadJson(strMsg);
cout<<"--------------------------------"<<endl; return ;
} int ReadJson(const std::string & strValue)
{
using namespace std; Json::Reader reader;
Json::Value value; if (reader.parse(strValue, value))
{
//解析json中的对象
string out = value["name"].asString();
cout << "name : " << out << endl;
cout << "number : " << value["number"].asInt() << endl;
cout << "value : " << value["value"].asBool() << endl;
cout << "no such num : " << value["haha"].asInt() << endl;
cout << "no such str : " << value["hehe"].asString() << endl; //解析数组对象
const Json::Value arrayNum = value["arrnum"];
for (unsigned int i = ; i < arrayNum.size(); i++)
{
cout << "arrnum[" << i << "] = " << arrayNum[i];
}
//解析对象数组对象
Json::Value arrayObj = value["array"];
cout << "array size = " << arrayObj.size() << endl;
for(unsigned int i = ; i < arrayObj.size(); i++)
{
cout << arrayObj[i];
}
//从对象数组中找到想要的对象
for(unsigned int i = ; i < arrayObj.size(); i++)
{
if (arrayObj[i].isMember("string"))
{
out = arrayObj[i]["string"].asString();
std::cout << "string : " << out << std::endl;
}
}
} return ;
} std::string writeJson()
{
using namespace std; Json::Value root;
Json::Value arrayObj;
Json::Value item;
Json::Value iNum; item["string"] = "this is a string";
item["number"] = ;
item["aaaaaa"] = "bbbbbb";
arrayObj.append(item); //直接对jsoncpp对象以数字索引作为下标进行赋值,则自动作为数组
iNum[] = ;
iNum[] = ;
iNum[] = ;
iNum[] = ;
iNum[] = ;
iNum[] = ; //增加对象数组
root["array"] = arrayObj;
//增加字符串
root["name"] = "json";
//增加数字
root["number"] = ;
//增加布尔变量
root["value"] = true;
//增加数字数组
root["arrnum"] = iNum; root.toStyledString();
string out = root.toStyledString(); return out;
}

4)在目录jsoncpp/ 下执行make命令

jsoncpp$ make
mkdir -p objs/src/json; mkdir -p objs/src;
g++ -c -Wall -Werror -g -I include src/json/json_reader.cpp -o objs/src/json/json_reader.o
g++ -c -Wall -Werror -g -I include src/json/json_value.cpp -o objs/src/json/json_value.o
g++ -c -Wall -Werror -g -I include src/json/json_writer.cpp -o objs/src/json/json_writer.o
g++ -c -Wall -Werror -g -I include src/main.cpp -o objs/src/main.o
g++ objs/src/json/json_reader.o objs/src/json/json_value.o objs/src/json/json_writer.o objs/src/main.o -o main

5)运行jsoncpp/下的main文件

./main

6)运行结果如下

fengbo: jsoncpp$ ./main
--------------------------------
json write :
{
"array" : [
{
"aaaaaa" : "bbbbbb",
"number" : ,
"string" : "this is a string"
}
],
"arrnum" : [ null, , , , , , ],
"name" : "json",
"number" : ,
"value" : true
} --------------------------------
json read :
name : json
number :
value :
no such num :
no such str :
arrnum[] = null
arrnum[] =
arrnum[] =
arrnum[] =
arrnum[] =
arrnum[] =
arrnum[] =
array size = {
"aaaaaa" : "bbbbbb",
"number" : ,
"string" : "this is a string"
}
string : this is a string
--------------------------------

作者:风波

mail : fengbohello@qq.com

jsoncpp封装和解析字符串、数字、布尔值和数组的更多相关文章

  1. 上篇:python的基本数据类型以及对应的常用方法(数字、字符串、布尔值)

    为了日后便于查询,本文所涉及到的必记的基本字符串方法如下: "分隔符".join(字符串)    #将字符串的每一个元素按照指定分隔符进行拼接.split("字符串&qu ...

  2. python学习3—数据类型之整型、字符串和布尔值

    python学习3-数据类型之整型.字符串和布尔值 数据类型 python3支持的数据类型共有6种: 1 Number 2 String 3 List 4 Tuple 5 Set 6 Dictiona ...

  3. JS规则 多样化的我(变量赋值)我们使用"="号给变量存储内容,你可以把任何东西存储在变量里,如数值、字符串、布尔值等,

    多样化的我(变量赋值) 我们可以把变量看做一个盒子,盒子用来存放物品,那如何在变量中存储内容呢? 我们使用"="号给变量存储内容,看下面的语句: var mynum = 5 ; / ...

  4. Python的基本数据数字、字符串、布尔值及其魔法

    基本数据类型介绍 若要把Pyhton的基本数据类型:数字(int).字符串(str).布尔(bool).列表(list).元组(tuple).字典(dict)都分为一个个不同的角色 如:战士,魔法师, ...

  5. python基础(数字、字符串、布尔值、字典数据类型简介)

    一 执行第一个python程序 1.下载安装python2.7和python3.6的版本及pycharm,我们可以再解释器中输入这样一行代码: 则相应的就打出了一句话.这里的print是打印的意思.你 ...

  6. js-数字、字符串、布尔值的转换方式

    来自JavaScript秘密花园 1.转换为字符串 '' + 10 === '10'; // true 将一个值加上空字符串可以轻松转换为字符串类型. 2.字符串转换为数字 +'010' === 10 ...

  7. Python基础之字符串,布尔值,整数,列表,元组,字典,集合

    一.str字符串 1.capitalize字符串首字母大写 name = "json" v = name.capitalize() print(v) # 输出结果:Json 2.c ...

  8. js中布尔值为false的六种情况

    下面6种值转化为布尔值时为false,其他转化都为true 1.undefined(未定义,找不到值时出现) 2.null(代表空值) 3.false(布尔值的false,字符串"false ...

  9. null, undefined 和布尔值

    说明:此类博客来自以下链接,对原内容做了标注重点知识,此处仅供自己学习参考! 来源:https://wangdoc.com/javascript/basic/introduction.html 1.n ...

随机推荐

  1. PHP 遍历数组的方法汇总

    1. foreach() foreach()是一个用来遍历数组中数据的最简单有效的方法. #example1: <?php $colors= array('red','blue','green' ...

  2. [转]Spring的IOC原理[通俗解释一下]

    1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...

  3. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  4. 多表利用DIH批量导入数据并建立索引注意事项

    如果希望同时对多个表进行全文检索,那我们该如何处理呢?利用DIH导入数据并建立索引时.schema.xml中配置了uniqueKey为id <uniqueKey>id</unique ...

  5. Java for LeetCode 189 Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  6. Android Design 4.4中文版发布

    “两年前的今天,我们发布了 Android Design 中文版(旧闻链接). 随着 Android 系统的发展,界面和设计语言都不断的发生变化.韶华易逝.光阴苒冉,Android 进化到了 4.4 ...

  7. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  8. Linux网络编程必看书籍推荐

    首先要说讲述计算机网络和TCP/IP的书很多. 先要学习网络知识才谈得上编程 讲述计算机网络的最经典的当属Andrew S.Tanenbaum的<计算机网络>第五版,这本书难易适中. &l ...

  9. hdu 1059 多重背包

    题意:价值分别为1,2,3,4,5,6的物品个数分别为a[1],a[2],a[3],a[4],a[5],a[6],问能不能分成两堆价值相等的. 解法:转化成多重背包 #include<stdio ...

  10. 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file

    转自:http://blog.csdn.net/david_xtd/article/details/7625626 前提:ubuntu-debug机器上向SVN提交了pdu-IVT,想在别的普通机器上 ...