Jsoncpp用法

虽然BCOS源码里有json-spirit的代码,但是我却发现它还使用了jsoncpp提供的东西,在这里对json和jsoncpp有一篇文章比较好,我附在这里,侵删:
https://www.cnblogs.com/kex1n/archive/2011/12/02/2272328.html
好啦:下面就是我自己的学习了:
和json-spirit一样jsoncpp也提供几个类:
Json::Value 表示值的类型
Json::Reader 对流或字符串解析成value
Json::Writer 将value转化成流或字符串
1.怎么读?

Json::Reader reader;  
Json::Value value;
reader.parase(str,value); 

其中str是要读取的流或者字符串,读到的东西就保存在value里,以后想读取json中的某一部分怎么办呢?
比如json是这样的

{   "ret":200,                          //http response code
    "code":0,                           //busi return code
    "info":"bad parameters"             //infomation of response 
} 

只要value[ret].asInt就可以读到它的值为200
value[info].asString就可以读到bad parameters
2.怎么写?

Json::Value Obj             //先创建一个json的对象
Json::Value item            //创建要插入对象的条目
item["ret"] = 200;          //赋值
obj.append(item)            //插入

// 转换为字符串(带格式)  
std::string out = Obj .toStyledString();  
// 输出无格式json字符串  
Json::FastWriter writer;  
std::string out2 = writer.write(Obj );  


JsonCpp是一个用于处理JSON数据的C++库。以下是使用JsonCpp的一些常见用法: 1. 解析JSON字符串 ```cpp #include <json/json.h> #include <iostream> #include <fstream> using namespace std; int main() { ifstream ifs("data.json"); Json::Value root; Json::CharReaderBuilder readerBuilder; JSONCPP_STRING errs; if (!Json::parseFromStream(readerBuilder, ifs, &root, &errs)) { std::cout << errs << std::endl; return 1; } // use root to access json data std::string name = root["name"].asString(); int age = root["age"].asInt(); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; return 0; } ``` 2. 生成JSON字符串 ```cpp #include <json/json.h> #include <iostream> int main() { Json::Value root; root["name"] = "John"; root["age"] = 30; Json::FastWriter writer; std::string json_str = writer.write(root); std::cout << json_str << std::endl; return 0; } ``` 3. 遍历JSON对象 ```cpp #include <json/json.h> #include <iostream> void printJson(Json::Value &root, int depth = 0) { if (root.isObject()) { std::cout << std::string(depth, ' ') << "{" << std::endl; for (auto it = root.begin(); it != root.end(); ++it) { std::cout << std::string(depth + 4, ' ') << it.name() << ": "; printJson(*it, depth + 4); } std::cout << std::string(depth, ' ') << "}" << std::endl; } else if (root.isArray()) { std::cout << std::string(depth, ' ') << "[" << std::endl; for (auto it = root.begin(); it != root.end(); ++it) { printJson(*it, depth + 4); } std::cout << std::string(depth, ' ') << "]" << std::endl; } else { std::cout << root.asString() << std::endl; } } int main() { std::string json_str = "{\"name\":\"John\",\"age\":30,\"hobbies\":[\"reading\",\"swimming\"]}"; Json::Value root; Json::CharReaderBuilder readerBuilder; JSONCPP_STRING errs; if (!Json::parseFromStream(readerBuilder, json_str, &root, &errs)) { std::cout << errs << std::endl; return 1; } printJson(root); return 0; } ``` 以上是JsonCpp的一些基本用法,更多用法可以参考JsonCpp的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值