jsoncpp封装和解析字符串、数字、布尔值和数组

本文涉及的产品
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
云解析 DNS,旗舰版 1个月
简介:

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

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

2)解压缩文件 jsoncpp.rar

unzip jsoncpp.rar

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

vim src/main.cpp
复制代码
  1 #include <string>
  2 #include <json/json.h>
  3 #include "stdio.h"
  4 
  5 int ReadJson(const std::string &);
  6 std::string writeJson();
  7 
  8 int main(int argc, char** argv)
  9 {
 10     using namespace std;
 11 
 12     std::string strMsg;
 13 
 14     cout<<"--------------------------------"<<endl;
 15     strMsg = writeJson();
 16     cout<< "json write : " << endl << strMsg << endl;
 17     cout<<"--------------------------------"<<endl;
 18     cout<< "json read :" << endl;
 19     ReadJson(strMsg);
 20     cout<<"--------------------------------"<<endl;
 21 
 22     return 0;
 23 }
 24 
 25 int ReadJson(const std::string & strValue) 
 26 {
 27     using namespace std;
 28 
 29     Json::Reader reader;
 30     Json::Value value;
 31 
 32     if (reader.parse(strValue, value))
 33     {
 34         //解析json中的对象
 35         string out = value["name"].asString();
 36         cout << "name : "   << out << endl;
 37         cout << "number : " << value["number"].asInt() << endl;
 38         cout << "value : "  << value["value"].asBool() << endl;
 39         cout << "no such num : " << value["haha"].asInt() << endl;
 40         cout << "no such str : " << value["hehe"].asString() << endl;
 41 
 42         //解析数组对象
 43         const Json::Value arrayNum = value["arrnum"];
 44         for (unsigned int i = 0; i < arrayNum.size(); i++)
 45         {
 46             cout << "arrnum[" << i << "] = " << arrayNum[i];
 47         }
 48         //解析对象数组对象
 49         Json::Value arrayObj = value["array"];
 50         cout << "array size = " << arrayObj.size() << endl;
 51         for(unsigned int i = 0; i < arrayObj.size(); i++)
 52         {
 53             cout << arrayObj[i];
 54         }
 55         //从对象数组中找到想要的对象
 56         for(unsigned int i = 0; i < arrayObj.size(); i++)
 57         {
 58             if (arrayObj[i].isMember("string")) 
 59             {
 60                 out = arrayObj[i]["string"].asString();
 61                 std::cout << "string : " << out << std::endl;
 62             }
 63         }
 64     }
 65 
 66     return 0;
 67 }
 68 
 69 std::string writeJson() 
 70 {
 71     using namespace std;
 72 
 73     Json::Value root;
 74     Json::Value arrayObj;
 75     Json::Value item;
 76     Json::Value iNum;
 77 
 78     item["string"]    = "this is a string";
 79     item["number"]    = 999;
 80     item["aaaaaa"]    = "bbbbbb";
 81     arrayObj.append(item);
 82 
 83     //直接对jsoncpp对象以数字索引作为下标进行赋值,则自动作为数组
 84     iNum[1] = 1;
 85     iNum[2] = 2;
 86     iNum[3] = 3;
 87     iNum[4] = 4;
 88     iNum[5] = 5;
 89     iNum[6] = 6;
 90 
 91     //增加对象数组
 92     root["array"]    = arrayObj;
 93     //增加字符串
 94     root["name"]    = "json";
 95     //增加数字
 96     root["number"]    = 666;
 97     //增加布尔变量
 98     root["value"]    = true;
 99     //增加数字数组
100     root["arrnum"]    = iNum;
101 
102     root.toStyledString();
103     string out = root.toStyledString();
104 
105     return out;
106 }
复制代码

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" : 999,
         "string" : "this is a string"
      }
   ],
   "arrnum" : [ null, 1, 2, 3, 4, 5, 6 ],
   "name" : "json",
   "number" : 666,
   "value" : true
}

--------------------------------
json read :
name : json
number : 666
value : 1
no such num : 0
no such str : 
arrnum[0] = null
arrnum[1] = 1
arrnum[2] = 2
arrnum[3] = 3
arrnum[4] = 4
arrnum[5] = 5
arrnum[6] = 6
array size = 1

{
    "aaaaaa" : "bbbbbb",
    "number" : 999,
    "string" : "this is a string"
}
string : this is a string
--------------------------------
复制代码

 


本文转自郝峰波博客园博客,原文链接:http://www.cnblogs.com/fengbohello/p/4066254.html,如需转载请自行联系原作者

相关文章
|
22天前
|
JavaScript
js 解析 byte数组 成字符串
js 解析 byte数组 成字符串
|
2月前
|
存储 关系型数据库 MySQL
|
3月前
|
SQL 开发框架 前端开发
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
|
2月前
|
存储 JavaScript 前端开发
一文带你深度解析:JavaScript中对象与数组的威力究竟有多大?
一文带你深度解析:JavaScript中对象与数组的威力究竟有多大?
|
3月前
|
自然语言处理 算法 搜索推荐
字符串相似度算法完全指南:编辑、令牌与序列三类算法的全面解析与深入分析
在自然语言处理领域,人们经常需要比较字符串,这些字符串可能是单词、句子、段落甚至是整个文档。如何快速判断两个单词或句子是否相似,或者相似度是好还是差。这类似于我们使用手机打错一个词,但手机会建议正确的词来修正它,那么这种如何判断字符串相似度呢?本文将详细介绍这个问题。
243 1
|
4月前
|
存储 算法 搜索推荐
深入解析String数组的操作与性能优化策略
深入解析String数组的操作与性能优化策略
|
4月前
|
Python
Python面向对象进阶:深入解析面向对象三要素——封装、继承与多态
Python面向对象进阶:深入解析面向对象三要素——封装、继承与多态
|
4月前
|
存储 算法 数据挖掘
深入解析力扣166题:分数到小数(模拟长除法与字符串操作详解及模拟面试问答)
深入解析力扣166题:分数到小数(模拟长除法与字符串操作详解及模拟面试问答)
|
3月前
|
存储 算法 搜索推荐
深入解析String数组的操作与性能优化策略
深入解析String数组的操作与性能优化策略
|
4月前
|
存储 Java 数据库
解析和使用String数组的方法
解析和使用String数组的方法

热门文章

最新文章

推荐镜像

更多
下一篇
无影云桌面