从 JSON 数组获取天气
Getting weather from JSON array
我对 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
. 我希望能够访问"天气"数组中的"描述"字段。 我该怎么做?
这行得通吗?
const Json::Value weather = root["weather"];
for ( int index = 0; index < weather.size(); ++index )
{
std::cout << weather[index]["description"].asString();
}
相关文章:
- 如何在 nlohmann 的 json 库中获取数组长度?
- 从用户那里获取数组大小的值
- 如何在VS2019中获取数组的任意对齐?
- 我如何获取数组的大小,以便我可以从函数声明所述数组
- 根据字符获取数组的索引
- 尝试递归获取数组元素的总和
- 用于获取数组长度的指针数学
- Socket.io客户端C++,从Socket.on获取数组
- C++ nlohmann JSON 获取数组名称
- 练习循环和使用循环 1 次以及如何获取数组的最大值
- 如何在函数参数中通过引用获取数组
- 如何获取数组中字符串元素的字符数?[C++]
- 如何声明一个未知大小的数组,然后输入直到我想要,然后获取数组的大小
- 无法获取数组内数组的总和
- 尝试获取数组中的最小元素时崩溃
- 获取数组类型的无效错误
- 如何从用户获取数组输入
- 使用位集获取数组项的条件总和,而无需使用循环
- 如何以模式获取数组中的数据?
- 如何获取数组中每个数字的平方根 (C++)