c++ 解析 {“hsm”:{“L1”:”1.1”,“b1”:”1.2”,“L2”:”1.3”,“b2”:”1.4”,“Le1”:”1.5”,“bo1”:”1.6”,“Le2”:”1.7”,“bo2”:”1.8”,“bu”:”1.9”,“val”:”2.0”}}
时间: 2023-08-10 17:04:34 浏览: 185
这是一个 JSON 格式的字符串,可以使用 C++ 中的 JSON 解析库来解析。下面给出一个使用 RapidJSON 库的示例代码:
```c++
#include <iostream>
#include "rapidjson/document.h"
using namespace rapidjson;
int main() {
const char* json_str = "{\"hsm\":{\"L1\":\"1.1\",\"b1\":\"1.2\",\"L2\":\"1.3\",\"b2\":\"1.4\",\"Le1\":\"1.5\",\"bo1\":\"1.6\",\"Le2\":\"1.7\",\"bo2\":\"1.8\",\"bu\":\"1.9\",\"val\":\"2.0\"}}";
Document d;
d.Parse(json_str);
const Value& hsm = d["hsm"];
const Value& L1 = hsm["L1"];
const Value& b1 = hsm["b1"];
const Value& L2 = hsm["L2"];
const Value& b2 = hsm["b2"];
const Value& Le1 = hsm["Le1"];
const Value& bo1 = hsm["bo1"];
const Value& Le2 = hsm["Le2"];
const Value& bo2 = hsm["bo2"];
const Value& bu = hsm["bu"];
const Value& val = hsm["val"];
std::cout << "L1: " << L1.GetString() << std::endl;
std::cout << "b1: " << b1.GetString() << std::endl;
std::cout << "L2: " << L2.GetString() << std::endl;
std::cout << "b2: " << b2.GetString() << std::endl;
std::cout << "Le1: " << Le1.GetString() << std::endl;
std::cout << "bo1: " << bo1.GetString() << std::endl;
std::cout << "Le2: " << Le2.GetString() << std::endl;
std::cout << "bo2: " << bo2.GetString() << std::endl;
std::cout << "bu: " << bu.GetString() << std::endl;
std::cout << "val: " << val.GetString() << std::endl;
return 0;
}
```
输出结果为:
```
L1: 1.1
b1: 1.2
L2: 1.3
b2: 1.4
Le1: 1.5
bo1: 1.6
Le2: 1.7
bo2: 1.8
bu: 1.9
val: 2.0
```
阅读全文