mardi 17 mars 2020

Feeding a nested JSON to rapidJSON document resulting in kParseErrorDocumentRootNotSingular

I'm preparing a function that will return the value of a key in a nested JSON. The problem is that I'm getting the nested JSON's content as a string and that is not something I can change.

For example: {"remote": {"ip": "127.0.0.1", "port": 58811}}

The content of "remote" is a nested JSON and my code needs to return the value of "ip". Upstream, this nested JSON is turned into a string and passed on to my function.

My code is:

string sampleInputVal = "\"remote\": {\"ip\": \"127.0.0.1\", \"port\": 58811}";
map<string, string> inputMap;

inputMap.emplace("key","remote");
inputMap.emplace("nestedKey","IP");

string getValNestedJSON(string inputVal, map<string, string> inputParameters)
{
    Document inputJSON;
    inputJSON.Parse(inputVal.c_str());

    return inputJSON[inputParameters["nestedKey"].c_str()].GetString();
}

A you can guess, sampleInputVal is just something I'm using to test the function, but I think the escape characters are messing up rapidjson because I'm getting kParseErrorDocumentRootNotSingular.

What should I do?

Aucun commentaire:

Enregistrer un commentaire