C++解析Json,使用JsonCpp读写Json数据

JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。通常用于数据交换或存储。

JsonCpp是一个基于C++语言的开源库,用于C++程序的Json数据的读写操作。

 

JsonCpp是一个开源库

下载地址:https://github.com/open-source-parsers/jsoncpp

文档地址:http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html

 

使用

官方提供的集成方案:https://github.com/open-source-parsers/jsoncpp/wiki/Amalgamated

其中最简单的方法是执行项目根目录中的python脚本,构建头文件和源文件。

1. 在安装Python环境的控制台中进入jsoncpp项目根目录,

2. 执行命令:

python amalgamate.py

3. 将生成的dist目录拷贝到自己的项目中,其中包源文件jsoncpp.cpp和头文件json.h、json-forwards.h

 

基本类和方法

使用jsoncpp库时需要包含头文件#include <json/json.h>(包含目录根据需要修改)

方法命名空间:Json

常用类

Value:用于存储Json数据

CharReader:从字符串中读取Json数据的抽象类

CharReaderBuilder:CharReader类的实现,可以读取标准输入流中的数据

 

Demo

定义Json数据结构,并输出数据

Json::Value jsonRoot; //定义根节点
Json::Value jsonItem; //定义一个子对象
jsonItem["item1"] = "one"; //添加数据
jsonItem["item2"] = 2;
jsonRoot.append(jsonItem); 
jsonItem.clear(); //清除jsonItem
jsonItem["item1.0"] = 1.0; jsonItem["item2.0"] = 2.0; jsonRoot["item"] = jsonItem; cout << jsonRoot.toStyledString() << endl; //输出到控制台

 

将Json写入到文件

ofstream ofs; //标准输出流
ofs.open("sample.json"); //创建文件
ofs << jsonRoot.toStyledString(); //输出
ofs.close();

 

从文件读取Json数据

ifstream ifs; //标准输入流
ifs.open("sample.json");
jsonRoot.clear();
Json::CharReaderBuilder builder; builder[
"collectComments"] = false; JSONCPP_STRING errs; if (!parseFromStream(builder, ifs, &jsonRoot, &errs)) //从ifs中读取数据到jsonRoot { return; }

 

从字符串读取Json数据

string jsonStr = jsonRoot.toStyledString(); //json字符串
jsonRoot.clear();
Json::CharReaderBuilder builder; builder[
"collectComments"] = false; JSONCPP_STRING errs; Json::CharReader* reader = builder.newCharReader(); if (!reader->parse(jsonStr.data(), jsonStr.data() + jsonStr.size(), &jsonRoot, &errs)) //从jsonStr中读取数据到jsonRoot { return; }

 

获取Json中的键值对

for (auto i = 0; i < jsonRoot.size(); i++)//遍历数组[]
{
    for (auto sub= jsonRoot[i].begin(); sub != jsonRoot[i].end(); sub++)//遍历对象{}
    {
        cout << sub.name() << " : " << jsonRoot[i][sub.name()] << endl; //方法1
cout << sub.name() << " : "
<< (*sub) << endl; //方法2
} }
eg:
jsonRoot= [ { "item1" : 1, "item2" : 2, }, { "a" : "a", "b" : 2.0, } ]
 

 

转载于:https://www.cnblogs.com/esCharacter/p/7657676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++使用JsonCpp库来读取JSON文件,你可以按照以下步骤进行操作: 1. 首先,确保已经下载并安装了JsonCpp库,并将其包含在你的项目中。 2. 创建一个Json::Value对象来存储解析后的JSON数据。 3. 使用Json::Reader对象来读取JSON文件并解析Json::Value对象。 下面是一个简单的示例代码,演示了如何使用JsonCpp读取JSON文件: ```cpp #include <iostream> #include <fstream> #include <json/json.h> int main() { // 读取 JSON 文件 std::ifstream jsonFile("example.json"); if (!jsonFile.is_open()) { std::cout << "Failed to open JSON file." << std::endl; return 1; } // 解析 JSON 数据 Json::Value root; Json::Reader reader; if (!reader.parse(jsonFile, root)) { std::cout << "Failed to parse JSON data." << std::endl; return 1; } // 读取 JSON 数据并输出 std::string name = root["name"].asString(); int age = root["age"].asInt(); std::string city = root["city"].asString(); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; return 0; } ``` 在上面的示例中,我们首先打开了一个名为`example.json`的JSON文件,然后使用JsonCppJson::Reader对象解析JSON数据,并将其存储在名为`root`的Json::Value对象中。接下来,我们通过访问`root`对象的键值对来获取JSON数据,并将其输出到控制台上。 请确保将示例代码中的`example.json`替换为你实际使用JSON文件的路径。 这里提供一个CSDN上的相关教程供参考:[https://blog.csdn.net/qq_41453285/article/details/105808014](https://blog.csdn.net/qq_41453285/article/details/105808014)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值