This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
How to properly read a Json string that may be null in some cases? #3135
Labels
kind: question
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
Hello,
I have some json format texts received as server response and would like to use this tool to parse them. The issue is if the value in a string field is optional and not provided from the server side, client side will receive a null rathe than an empty string. This currently throws exceptions while parsing.
For example this is the code where we read the title of a layer.
bh.layers[j].name = js.at(i)["th_layers"].at(j)["layer_title"].get<string>().c_str();
However, in the json format, sometime the server response could be this. And this can be parsed without any issue.
"layer_title": "Fill Material",
But sometimes it becomes this and throws an exception.
"layer_title": null,
Is there a better way of parsing this null and give a default value(like "" for string and 0 for int) rather than using try and catch?
Thanks a lot!
The text was updated successfully, but these errors were encountered: