我正在尝试遍历 JSON 中的对象列表以查找具有匹配 KVP 的对象(在 C++ 中使用 RapidJSON)。我已经设法使用硬编码指针检索值,但无法让函数GetValueByPointer(document, "PointerString")接受我正在构建的动态字符串。
JSON 看起来像这样:
{ "_id" : { "$oid" : "5d0985973f1c0000ee000000" },
"Location" : [ { "lat" : "39.4005", "lon" : "-106.106"} ],
"Weather" : [ { "timestamp" : "2019-06-05T00:00:00", ...}, { "timestamp" : "2019-06-05T01:00:00", ...}}
这有效:
Document document;
document.Parse(json);
Value* a = GetValueByPointer(document, "/Weather/1/timestamp");
std::cout << a->GetString() << std::endl;
这不起作用:
Value* a = GetValueByPointer(document, "/Weather/1/timestamp");
int i = 1;
std::string base = "/Weather/";
std::string tail = "/timestamp";
std::string PointerString;
std::string TSString = "";
while(TSString != "2019-06-05T09:00:00") {
PointerString=base;
PointerString.append(std::to_string(i));
PointerString.append(tail);
PointerString = "\"" + PointerString + "\"";
Value* timestamp = GetValueByPointer(document, PointerString);
TSString = timestamp->GetString();
std::cout << TSString << std::endl;
i++;
}
无论我尝试将我的 PointerString 转换为什么,我得到的错误是:
/usr/local/include/rapidjson/pointer.h:1156:30: note: template argument deduction/substitution failed:
MGOIO.cc:145:62: note: mismatched types ‘const CharType [N]’ and ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’
Value* timestamp = GetValueByPointer(document, PointerString);
^
当我输出PointerString到屏幕时,它对我来说看起来不错:
"/Weather/1/timestamp"
非常感谢任何帮助!