Open AVV86 opened 6 years ago
I tried to use JSON Pointer in rapidjson to implement what you may want:
#include "rapidjson/pointer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
void DumpPointers(const Value& v, const Pointer& parent) {
if (v.IsObject())
for (Value::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
DumpPointers(itr->value, parent.Append(itr->name.GetString(), itr->name.GetStringLength()));
else if (v.IsArray())
for (SizeType i = 0; i < v.Size(); i++)
DumpPointers(v[i], parent.Append(i));
else {
StringBuffer sb;
parent.Stringify(sb);
std::cout << sb.GetString() << std::endl;
}
}
int main() {
const char *temp = "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta- markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\"] }, \"GlossSee\": \"markup\"} } } }}";
Document d;
d.Parse(temp);
Pointer root;
DumpPointers(d, root);
}
Output:
/glossary/title
/glossary/GlossDiv/title
/glossary/GlossDiv/GlossList/GlossEntry/ID
/glossary/GlossDiv/GlossList/GlossEntry/SortAs
/glossary/GlossDiv/GlossList/GlossEntry/GlossTerm
/glossary/GlossDiv/GlossList/GlossEntry/Acronym
/glossary/GlossDiv/GlossList/GlossEntry/Abbrev
/glossary/GlossDiv/GlossList/GlossEntry/GlossDef/para
/glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/0
/glossary/GlossDiv/GlossList/GlossEntry/GlossDef/GlossSeeAlso/1
/glossary/GlossDiv/GlossList/GlossEntry/GlossSee
Thank you @miloyip . One more question , Can the pointer be used to retrieve value directly from the json without iterating?
Hello , I am trying to write a code to get the keys and the path of the key which can be used further in another function.I am able to get the keys in the json , but I am unable to retrieve the path of those keys.I need the path of the keys to avoid the recursion in further implementation.
Json file
Output keys
These are the keys whose real path I want to retrive glossary title GlossList ID SortAs GlossTerm Acronym Abbrev GlossDef GlossSee
Note : This is just a sample json but the json that will be received is unknown to me , so i have to get the keys and key path of that json .