gpt4 book ai didi

c++ - 如何在 C++ 中使用 rapidjson 库从 json 格式字符串中获取值

转载 作者:行者123 更新时间:2023-11-30 05:44:04 24 4
gpt4 key购买 nike

我正在尝试使用 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<>; Allocator = 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> .

最佳答案

const Value& a = document["values"];
StringBuffer buf;
PrettyWriter<StringBuffer> wr(buf);
a.Accept(wr);
const char* js = buf.GetString();

关于c++ - 如何在 C++ 中使用 rapidjson 库从 json 格式字符串中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29957825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com