jsoncpp解析

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

#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 0;
}

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 = 0; i < arrayNum.size(); i++)
        {
            cout << "arrnum[" << i << "] = " << arrayNum[i];
        }
        //解析对象数组对象
        Json::Value arrayObj = value["array"];
        cout << "array size = " << arrayObj.size() << endl;
        for(unsigned int i = 0; i < arrayObj.size(); i++)
        {
            cout << arrayObj[i];
        }
        //从对象数组中找到想要的对象
        for(unsigned int i = 0; i < arrayObj.size(); i++)
        {
            if (arrayObj[i].isMember("string")) 
            {
                out = arrayObj[i]["string"].asString();
                std::cout << "string : " << out << std::endl;
            }
        }
    }
    return 0;
}

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"]    = 999;
    item["aaaaaa"]    = "bbbbbb";
    arrayObj.append(item);
    //直接对jsoncpp对象以数字索引作为下标进行赋值,则自动作为数组
    iNum[1] = 1;
    iNum[2] = 2;
    iNum[3] = 3;
    iNum[4] = 4;
    iNum[5] = 5;
    iNum[6] = 6;
    //增加对象数组
    root["array"]    = arrayObj;
    //增加字符串
    root["name"]    = "json";
    //增加数字
    root["number"]    = 666;
    //增加布尔变量
    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" : 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值