使用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. Two Sum I & II

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  2. 如何分割一个utf8字符串(保证单个汉字的完整性)

    std::list<std::string> split_utf8_string(const std::string& text) { std::list<std::stri ...

  3. Ribbon_窗体_实现Ribbon风格的窗体

    Ribbon_窗体_实现Ribbon风格的窗体 随着office2007的兴起,微软让我们看到了Ribbon风格的窗体,现在很多软件也都开始使用Ribbon风格.那么我们如果要自己开发,应当怎么做呢? ...

  4. DP:Islands and Bridges(POJ 2288)

    2015-09-21 造桥基建工程 题目大意,就是有n座岛和k座桥,要你找一条哈密顿圈(找完所有的岛,并且每个岛只经过一次),当经过一座岛就加上岛的价值,如果两岛联通,则加上两座岛的价值之积,如果三座 ...

  5. svn Error: post-commit hook failed (exit code 127) with output

    Command: Commit Modified: C:\Users\xsdff\Desktop\project\index.html Sending content: C:\Users\xsdff\ ...

  6. IIS7 / IIS7.5 URL 重写 HTTP 重定向到 HTTPS(转)

    转自: http://www.cnblogs.com/yipu/p/3880518.html   1.购买SSL证书,参考:http://www.cnblogs.com/yipu/p/3722135. ...

  7. 数据库优化和SQL操作的相关题目

    SQL操作 1.有一个数据库表peope,表有字段name,age,address三个属性(注:没有主键).现在如果表中有重复的数据,请删去重复只留下其中的一条.重复的定义就是两条记录的name,ag ...

  8. jquery easy ui 1.3.4 数据表格(DataGrid)(8)

    8.1.创建DataGrid html代码 <table id="dg"></table> $("#dg").datagrid({ // ...

  9. jQuery Mobile 基础(第四章)

    1.主题 jQuery Mobile 提供了5种不同的主题样式, 从 "a" 到 "e" - 每一种主题的按钮,工具条,内容块等等颜色都不一致,每个主题的视觉效 ...

  10. Java Hour 39 Maven ( 1 )

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 39 Maven 1 Perhaps you are running ...