ofJson from string / ofxJSONRPC results


#1

I am using ofxJsonRPC and have a question. I get my response from clients (based on the example through this line of code:

ofLogVerbose("ofApp::setText") << args.params.dump(4);

It dumps the JSON file as a string. However it contains / that I guess are used to escape the " but it is a string.

so I get this:

{\"userid\":\"+123456789\",\"question\":3,\"answer\":\"B\"}

instead of:

{"userid":"+123456789","question":3,"answer":"B"}

If I try to parse args.params, or args.results I don’t get any data, however the string dump shows I have the data coming back.

How can I either convert a string, already correctly formatted like this:

{"userid":"+123456789","question":3,"answer":"B"}

to ofJson,?

Or better yet how can I parse the data that returns from ofxJSONRPC with ofJson?


#2

args.params is an ofJson object … and an ofJson object is just a nlohmann::json:

So you can just use all of the normal accessors that are included with nlohmann::json

e.g.

std::cout << args.params["userid"] << std::endl;

should print the userid …


#3

OR … perhaps ofxJSONRPC is not parsing the string as JSON but just saving it as a single string in ofJson?

You can test it out by running code like above. If you can’t get individual values, the json probably isn’t getting parsed correctly in ofxJSONRPC and we should check on that …


#4

Hi, I guess my first idea was correct, there is something amis with the formatting. It seems the escape characters are not handled correctly in Json file that I get on the OF side, and it has a \ before every "

Treating it as a Json file does not work as it is valid (by coincidence) but not useable.