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

 

#include <iostream>
#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);
	item.clear();
	item["heha"] = "hello";
	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;
}

运行结果:

--------------------------------
json write : 
{
   "array" : [
      {
         "aaaaaa" : "bbbbbb",
         "number" : 999,
         "string" : "this is a string"
      },
      {
         "heha" : "hello"
      }
   ],
   "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] = nullarrnum[1] = 1arrnum[2] = 2arrnum[3] = 3arrnum[4] = 4arrnum[5] = 5arrnum[6] = 6array size = 2
{
	"aaaaaa" : "bbbbbb",
	"number" : 999,
	"string" : "this is a string"
}{
	"heha" : "hello"
}string : this is a string
--------------------------------

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值