C++ ----device-stream-boundary-- Content-Type: application/json Content-Length: 75 X-If-Encrypt: 0 {"type":"response", "seq":0, "params":{"error_code":0, "session_id":"157"}} 字符串解析出session_id
时间: 2023-07-08 07:49:03 浏览: 149
c/c++ 解析JSON数据
5星 · 资源好评率100%
可以使用 JSON 解析库来解析该字符串并获取 session_id。以下是一个使用 RapidJSON 库的示例代码:
```c++
#include <iostream>
#include "rapidjson/document.h"
int main() {
const char* json_str = R"({"type":"response", "seq":0, "params":{"error_code":0, "session_id":"157"}})";
rapidjson::Document doc;
doc.Parse(json_str);
if (!doc.IsObject() || !doc.HasMember("params")) {
std::cout << "Invalid JSON format!" << std::endl;
return -1;
}
const rapidjson::Value& params = doc["params"];
if (!params.IsObject() || !params.HasMember("session_id") || !params["session_id"].IsString()) {
std::cout << "Missing or invalid session_id field!" << std::endl;
return -1;
}
std::string session_id = params["session_id"].GetString();
std::cout << "Session ID: " << session_id << std::endl;
return 0;
}
```
输出结果为:
```
Session ID: 157
```
阅读全文