如何从Json获得价值
How to get value from Json
本文关键字:Json 更新时间:2023-10-16
我需要从一个有toJson()的Json AdSpot中获取值(String或int),我需要通过键"ext"(它也是一个Json)检索字段,然后通过键"isBanner"从ext.的Json值中检索字段
这是Json AdSpot:
AdSpot(OpenRTB::Impression && imp)
: OpenRTB::Impression(std::move(imp))
{
}
void fromJson(const Json::Value & val);
Json::Value toJson() const;
我尝试使用get,但不知道该为默认值传递什么参数。
您将从jsoncpp文档中找到答案
从Json::Value中,您可以使用将其作为字符串获取
std::string asString()const
或使用作为整数
Int asInt()常量
然后,您的问题中的JSON导航可以使用:
Json::Value extValue = value["ext"]; Json::Value isBannerValue = extValue["isBanner"]; std::string isBanner = isBannerValue.asString();
如果不能强制转换,则会引发异常。
相关文章:
- 在c++中使用nlohmann从类到json的转换
- C++json插入数组
- nlohmann-json将一个数组插入到另一个数组中
- 如何使用curlpp通过POST方法上传文件和json数据
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 nlohmann 的 json 库中获取数组长度?
- UE4 C++ 我无法将 JSON 嵌套值获取到 TArray
- 如何在特定位置追加 json 文件
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 使用 jsoncpp 解析 json 数组字符串
- 将 FlatBuffer 对象序列化为 JSON,而不使用其架构文件
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 使用 cmake 生成 compile_commands.json
- 如何使用 c++ 和提升库生成 json
- 使用 minijson-reader 库读取 JSON 字符串时出现问题
- 使用 Qt 解析 JSON 数组
- 如何使用 picojson.h 在 c++ 中从头开始创建 json 对象
- 在 C++ 中访问 JSON 数组值