RapidJson 和来自 Yahoo Weather 的数据

RapidJson and Data from Yahoo Weather

我是 RapidJson 的新手,我需要使用它构建一个应用程序。

目前我拥有的是从 Yahoo API Weather 获得的 JSon 字符串,它看起来像这样 :

{
  query: {
        count: 1,
        created: "2015-10-21T20:40:02Z",
        lang: "fr-FR",
        results: {
              channel: {
                    item: {
                          condition: {
                                code: "27",
                                date: "Wed, 21 Oct 2015 9:59 pm CEST",
                                temp: "59",
                                text: "Mostly Cloudy"
                          }
                    }
              }
        }
  }
}

我处理数据的函数在这里:

void MyCurl::dataHandle(const std::string &data)
{
  //  std::cout << data << std::endl;                                                 

  Document d;
  d.Parse(data.c_str());

  std::cout << d["temp"].GetString() << std::endl;
}

最后一行抛出:

rapidjson/document.h :866 : rapidjson::GenericValue<Encoding, Allocator>& rapidjson::GenericValue<Encoding, Allocator>::operator[](const rapidjson::GenericValue<Encoding, SourceAllocator>&) [with SourceAllocator = rapidjson::MemoryPoolAllocator<>; Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: assertion <false> failed.

如何获取温度和文本?

感谢您的帮助!

JSon 的格式有点特殊,我只需要把所有的数组都挖出来。

所以现在我的行​​看起来像:

std::cout << d["query"]["results"]["channel"]["item"]["condition"]["text"].GetString() << std::endl; 

以上字符串不是有效的 json 格式。你需要逃避它。而不是 { temp : 23 } 它必须是 string json ="{/"temp/" : 23}";

此外,您可以使用 HasParseError()IsString() 在 RapidJson 中执行错误处理。