我正在尝试使用 C++ 中的 rapidjson 库从以下 json 格式字符串中迭代值。我得到了这个 json 格式字符串来响应来自 Allegro Graph Server 的查询。
json格式如下:
{"names":["s","pred","o"],"values":[["<http://www.example.com/ns/node2>","<http://www.example.com/ns/connectWith>","<http://www.example.com/ns/node5>"],["<http://www.example.com/ns/node3>","<http://www.example.com/ns/connectWith>","<http://www.example.com/ns/node4>"],["<http://www.example.com/ns/node6>","<http://www.example.com/ns/connectWith>","<http://www.example.com/ns/node1>"]]}
我试图用 rapidjson 教程示例迭代他们的示例 json 字符串,它没有问题。但是,当我传递上面的数据时,编译器抱怨如下: rapidjson::GenericValue::GetInt() const [with Encoding = rapidjson:: UTF8<>; 分配器 = rapidjson::MemoryPoolAllocator<>]:断言 `flags_ & kIntFlag' 失败
我使用下面的代码来迭代
// I have added \ before * in the following data to make string
const char json[]= " {\"names\":[\"s\",\"pred\",\"o\"],\"values\":[[\"<http://www.example.com/ns/node2>\",\"<http://www.example.com/ns/connectWith>\",\"<http://www.example.com/ns/node5>\"],[\"<http://www.example.com/ns/node3>\","<http://www.example.com/ns/connectWith>\",\"<http://www.example.com/ns/node4>\"],[\"<http://www.example.com/ns/node6>\",\"<http://www.example.com/ns/connectWith>\",\"<http://www.example.com/ns/node1>\"]]}";
printf("Original JSON:\n %s\n", json);
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
#if 0
// "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
if (document.Parse(json).HasParseError())
return 1;
#else
// In-situ parsing, decode strings directly in the source string. Source must be string.
char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json));
if (document.ParseInsitu(buffer).HasParseError())
return ;
#endif
printf("\nParsing to document succeeded.\n");
// 2. Access values in document.
printf("\nAccess values in document:\n");
assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
assert(document.HasMember("names"));
// Using a reference for consecutive access is handy and faster.
const Value& a = document["names"];
for (SizeType i = 0; i < a.Size(); i++) // Uses SizeType instead of size_t
printf("a[%d] = %d\n", i, a[i].GetInt());
任何人都可以帮助我如何从 json 字符串中获取值吗?
预期的输出如下所示,类似于 .nt 文件,一行中的每个数组值都跟在点后面。
<http://www.example.com/ns/node2> <http://www.example.com/ns/connectWith> <http://www.example.com/ns/node5> .
<http://www.example.com/ns/node3> <http://www.example.com/ns/connectWith> <http://www.example.com/ns/node4> .
<http://www.example.com/ns/node6> <http://www.example.com/ns/connectWith> <http://www.example.com/ns/node1> .