用c++的rapidjson解析出{"tch":{"on":1,"get":65}}中的“”里内容和相应的值
时间: 2023-12-04 10:41:50 浏览: 136
以下是使用C++的rapidjson解析出给定JSON字符串中的键和值的代码示例:
```c++
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace rapidjson;
int main() {
const char* json = "{\"tch\":{\"on\":1,\"get\":65}}";
Document document;
document.Parse(json);
const Value& tch = document["tch"];
const Value& on = tch["on"];
const Value& get = tch["get"];
std::cout << "on: " << on.GetInt() << std::endl;
std::cout << "get: " << get.GetInt() << std::endl;
return 0;
}
```
输出:
```
on: 1
get: 65
```
相关问题
用c++的rapidjson解析出类似{"tch":{"on":1,"get":65}}中所有内容,{"on":1,"get":65}中不止2个,可能含有多个键值对
以下是使用C++的rapidjson解析JSON字符串的示例代码:
```cpp
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <string>
using namespace rapidjson;
using namespace std;
int main() {
// 定义JSON字符串
string jsonStr = "{\"tch\":{\"on\":1,\"get\":65}}";
// 解析JSON字符串
Document doc;
doc.Parse(jsonStr.c_str());
// 遍历JSON对象
const Value& obj = doc["tch"];
for (Value::ConstMemberIterator itr = obj.MemberBegin(); itr != obj.MemberEnd(); ++itr) {
const char* key = itr->name.GetString();
const Value& value = itr->value;
if (value.IsInt()) {
cout << key << ": " << value.GetInt() << endl;
}
}
return 0;
}
```
输出结果为:
```
on: 1
get: 65
```
阅读全文