David Change
David Change

Reputation: 29

How to get value from Json

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.

Upvotes: 0

Views: 6454

Answers (1)

mpromonet
mpromonet

Reputation: 11942

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.

Upvotes: 1

Related Questions