c++ - 从 JSON 数组获取天气

标签 c++ json jsoncpp

我对 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();
}

关于c++ - 从 JSON 数组获取天气,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37360294/

相关文章:

javascript - 通过环境变量获取 MQTT 节点密码

c++ - JSONCPP 写入文件

c++ - 使用 jsoncpp 创建空的 json 数组

c++ - visual studio 控件编辑器为 visual c++ 生成的代码在哪里?

c++ - ICU 与 C++ 中的 Boost 语言环境

c++ - 为什么 union 静态成员不存储为 union ?

json - 如何使用 Spring WebClient 按名称获取 json 字段?

c++ - 是否可以在 CEdit 控件的纯数字输入模式和字母数字输入模式之间切换?

javascript - 从 URL 检索 JSON

c++ - 如何将 JsonCPP 值作为字符串获取?