加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:33:07 所属栏目:百科 来源:网络整理
导读:使用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

使用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<< 18     cout<< json read :" << endl;
 19     ReadJson(strMsg);
 20     cout<< 21 
 22     return 0;
 23 }
 24 
 25 string & strValue) 
 26 {
 27      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 : haha 40         cout << no such str : 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 0; i < arrayObj.size(); i++)
 52         {
 53             cout << arrayObj[i];
 54         }
 55         从对象数组中找到想要的对象
 56          57         {
 58             if (arrayObj[i].isMember(string")) 
 59             {
 60                 out = arrayObj[i][ 61                 std::cout << string : " << out << std::endl;
 62             }
 63         }
 64     }
 65 
 66      67 }
 68 
 69 std::string writeJson() 
 70 {
 71      72 
 73     Json::Value root;
 74     Json::Value arrayObj;
 75     Json::Value item;
 76     Json::Value iNum;
 77 
 78     item["]    = this is a string";
 79     item[999;
 80     item[aaaaaabbbbbb 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["]    = arrayObj;
 93     增加字符串
 94     root[json 95     增加数字
 96     root[666;
 97     增加布尔变量
 98     root["]    = true;
 99     增加数字数组
100     root["]    = iNum;
101 
102     root.toStyledString();
103     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 : 
{
   " : [
      {
         " : ",999,0); line-height:1.5!important">"
      }
   ],0); line-height:1.5!important">" : [ null,1,128); line-height:1.5!important">2,128); line-height:1.5!important">3,128); line-height:1.5!important">4,128); line-height:1.5!important">5,128); line-height:1.5!important">6 ],128); line-height:1.5!important">666,0); line-height:1.5!important">" : true
}

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

{
    "
}
string : this is a string
--------------------------------

作者:风波

mail : fengbohello@qq.com

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读