我对c ++非常陌生,我正在尝试使用jsoncpp来解决数组中的天气。
json字符串如下所示:
{"coord":{"lon":139,"lat":35},
"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
"wind":{"speed":7.31,"deg":187.002},
"rain":{"3h":0},
"clouds":{"all":92},
"dt":1369824698,
"id":1851632,
"name":"Shuzenji",
"cod":200}
解析json数组工作正常,这是相关的代码:
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse( data.c_str(), root );
if ( !parsingSuccessful )
{
std::cout << "Failed to parse"
<< reader.getFormattedErrorMessages();
return 0;
}
std::cout << root.get("description", "n/a" ).asString() << std::endl;
但我仍以n/a
结束。我希望能够访问&#34;描述&#34;在&#34;天气&#34;阵列。我怎么能这样做?
答案 0 :(得分:1)
这有用吗?
const Json::Value weather = root["weather"];
for ( int index = 0; index < weather.size(); ++index )
{
std::cout << weather[index]["description"].asString();
}