用c++的rapidjson解析出类似{"tch":{"on":1,"get":65}}中所有内容,{"on":1,"get":65}中不止2个,可能含有多个键值对
时间: 2023-12-04 13:41:52 浏览: 22
以下是使用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
```