[{"deliveryTime":1686009600000,"productId":"1085769714946367489","shippingQuantity":1,"status":true},{"deliveryTime":1686096000000,"productId":"1085769714946367489","shippingQuantity":1,"status":true},{"deliveryTime":1686182400000,"productId":"1085769714946367489","shippingQuantity":1,"status":true},{"deliveryTime":1686268800000,"productId":"1085769714946367489","shippingQuantity":1,"status":true},{"deliveryTime":1686355200000,"productId":"1085769714946367489","shippingQuantity":1,"status":true}]怎么提取这段string中deliveryTime的值?
时间: 2023-07-19 10:55:35 浏览: 38
可以使用 JSON 解析库将这个 string 解析为 JSON 数组,然后遍历数组,提取每个元素中的 deliveryTime 值。
以下是一个使用第三方库 `nlohmann/json` 的示例代码:
```c++
#include <iostream>
#include <string>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
std::string input = "[{\"deliveryTime\":1686009600000,\"productId\":\"1085769714946367489\",\"shippingQuantity\":1,\"status\":true},{\"deliveryTime\":1686096000000,\"productId\":\"1085769714946367489\",\"shippingQuantity\":1,\"status\":true},{\"deliveryTime\":1686182400000,\"productId\":\"1085769714946367489\",\"shippingQuantity\":1,\"status\":true},{\"deliveryTime\":1686268800000,\"productId\":\"1085769714946367489\",\"shippingQuantity\":1,\"status\":true},{\"deliveryTime\":1686355200000,\"productId\":\"1085769714946367489\",\"shippingQuantity\":1,\"status\":true}]";
json j = json::parse(input);
for (const auto& item : j) {
std::cout << "deliveryTime: " << item["deliveryTime"].get<int64_t>() << std::endl;
}
return 0;
}
```
这段代码将字符串 input 解析为一个 JSON 数组,然后遍历数组中的每个元素,并输出其 deliveryTime 值。注意,因为 deliveryTime 的值是一个整数,所以需要使用 `get<int64_t>()` 方法来获取它的值。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)