0

I need to get the value(String or int) from a Json AdSpot which has a toJson(), I need to retrieve the field by key "ext" (it's also a Json), then retrieve field by key "isBanner" from the Json value of ext.

Here is the Json AdSpot:

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

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

I tried to use get, but don't know what to pass in the parameter for the default value.

mpromonet
  • 11,326
  • 43
  • 62
  • 91
David Change
  • 29
  • 1
  • 7

1 Answers1

1

You will find the answer from jsoncpp documentation
From a Json::Value you can get it as a string using

std::string asString () const

or as a intger using

Int asInt () const

Then the JSON navigation in your question could be done with :

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

If it cannot be cast it will raise an exception.

mpromonet
  • 11,326
  • 43
  • 62
  • 91