JsonCpp 的使用

前端开发 作者: 2024-08-21 03:35:01
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读、编写、解析。jsoncpp是c++解析JSON串常用的解析库之一。   jsoncpp中主要的类: Json::Value:可以表示所有支持的类型,如:int , double ,string , object, array等。其包含节点的类型判断(isNull,isBool,isInt,is
Json::Reader *pJsonParser = new Json::Reader(Json::Features::strictMode());
1.value.isMember("key");    //存在返回true,否则为false
2.value["sex"].isNull();    //为NULL返回1,否则为0
#include <iostream>
#include <sstream>
#include <fstream>
#include <json/json.h>

void readJsonFromFile()
{
    std::ifstream ifs;
    ifs.open("a.json");
    std::stringstream buffer;
    buffer << ifs.rdbuf();
    ifs.close();

    auto str = buffer.str();

    Json::Reader reader;
    Json::Value value;
    if (reader.parse(str,value)) {
        //节点判断
        std::cout << "value‘s empty:" << value.empty() << std::endl;
        std::cout << "name is string:" << value["name"].isString() << std::endl;
        std::cout << "age is string:" << value["age"].isString() << std::endl;

        //类型获取
        std::cout << "name‘s type:" << value["name"].type() << std::endl;
        std::cout << "like‘s type:" << value["like"].type() << std::endl;

        //类型转换
        //根据Key获取值时最好判断类型,否则解析会中断
        std::cout << "name:" << value["name"].asString() << std::endl;
        std::cout << "age:" << value["age"].asInt() << std::endl;

        //节点获取
        std::cout << value["job"] << std::endl;                        //[]方式获取
        std::cout << value.get("name","dxx") << std::endl;            //get方式获取
        std::cout << value.isMember("job") << std::endl;
        std::cout << "value‘s obj:" << value.isObject() << std::endl;
        std::cout << "like‘s obj:" << value["like"].isObject() << std::endl;

        std::cout << "like.size:" << value["like"].size() << std::endl;
        std::cout << "like[0][food]:" << value["like"][0]["food"].asString() << std::endl;

        //节点操作
        std::cout << "name compare age:" << value["name"].compare("age") << std::endl;
        value["name"] = "swduan";            //修改
        value["address"] = "hz";             //增加
        value["phone"] = "10086";        
        value.removeMember("age");           //删除
        value["like"][0]["sport"] = "game";  //往value["like"]中添加一项元素

        Json::Value item;
        item["hate"] = "game";
        value["like"].append(item);            //value["like"]中再添加一维数组
        std::cout << "value[\"like\"]‘s size:" << value["like"].size() << std::endl;
        
        std::cout << "--------------------" << std::endl;
        std::cout << value.toStyledString() << std::endl;

        std::cout << "--------------------" << std::endl;
        auto all_member = value.getMemberNames();
        for (auto member : all_member) {
            std::cout << member << std::endl;
        }

        std::cout << "--------------------" << std::endl;
        value.clear();        //清空元素
        std::cout << value.toStyledString() << std::endl;
    }
}

void jsonWriteToFile()
{
    Json::FastWriter write;
    Json::Value root;

    Json::Value item;
    Json::Value arrayObj;
    item["book"] = "c++";
    item["food"] = "apple";
    item["music"] = "ddx";
    arrayObj.append(item);

    root["name"] = "dsw";
    root["age"]  = 18;
    root["like"] = arrayObj;    //注意:这里不能用append,append功能是将Json::Value添加到数组末尾

    auto str = root.toStyledString();
    std::cout << str << std::endl;

    std::ofstream ofss;
    ofss.open("a.json");
    ofss << str;
    ofss.close();
}

int main()
{
    jsonWriteToFile();
    readJsonFromFile();

    getchar();
    return 0;
}
values empty:0
name is string:1
age is string:0
names type:4
likes type:6
name:dsw
age:18
null
"dsw"
values obj:1
likes obj:0
like.size:1
like[0][food]:apple
name compare age:1
value["like"]s size:2
--------------------
{
   "address" : "hz","job" : null,"like" : [
      {
         "book" : "c++","food" : "apple","music" : "ddx","sport" : "game"
      },{
         "hate" : "game"
      }
   ],"name" : "swduan","phone" : "10086"
}

--------------------
address
job
like
name
phone
--------------------
{}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65875.html
jsoncpp 使用