0

我需要从具有 toJson() 的 Json AdSpot 中获取值(字符串或 int),我需要通过键“ext”(它也是一个 Json)检索字段,然后通过键“isBanner”检索字段分机的 Json 值。

这是 Json AdSpot:

AdSpot(OpenRTB::Impression && imp)
    : OpenRTB::Impression(std::move(imp))
{
}

void fromJson(const Json::Value & val);
Json::Value toJson() const;

我尝试使用get,但不知道在参数中传递什么作为默认值。

4

1 回答 1

1

您将从jsoncpp 文档
中找到答案 From a Json::Value 您可以使用字符串将其作为字符串获取

std::string asString () 常量

或作为整数使用

整数 asInt () 常量

然后可以通过以下方式完成您问题中的 JSON 导航:

Json::Value extValue = value["ext"];
Json::Value isBannerValue = extValue["isBanner"];
std::string isBanner = isBannerValue.asString();

如果无法强制转换,则会引发异常。

于 2014-10-24T19:14:50.737 回答