Reputation: 139
I am trying to iterate the values from the following json format string using rapidjson library in C++. I have got this json format string in response to query from Allegro Graph Server.
The json format is as follows:
{"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>"]]}
I have tried to iterate their sample json string with rapidjson tutorial example that it works no problem.However when I pass my above data, the compilers complains as follows: rapidjson::GenericValue::GetInt() const [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `flags_ & kIntFlag' failed
I use the following code to iterate through
// 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());
Could any one please help me out how I could get the values from json string?
The expected output is as follows like .nt file, each array value in one row follow with dot.
<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> .
Upvotes: 1
Views: 6804
Reputation: 139
const Value& a = document["values"];
StringBuffer buf;
PrettyWriter<StringBuffer> wr(buf);
a.Accept(wr);
const char* js = buf.GetString();
Upvotes: 2