-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing error when there is a json string within a Json #3445
Comments
What is the what() message of the exception? |
Please show a complete example. It works just fine here: https://godbolt.org/z/sjGq79Pnn
|
Just as the Error Messages says, try{
auto req = nlohmann::json::parse(data, data+size); // data is std::int8_t *
}catch (std::exception e){
std::cout << "error: " << e.what();
} |
@falbrechtskirchinger |
@falbrechtskirchinger @nlohmann {"some_key":"{\"in_key\":\"in_value\" }"} {"some_key":
"{\"in_key\":\"in_value\" }"
} Following are incorrect: {"some_key":
"{\
\"in_key\":\"in_value\"\
}"
} {
"some_key":"{"in_key":"in_value" }"
} {"some_key":"{\"in_key\":\"in_value\"\
}"
} {"some_key":"{
\"in_key\":\"in_value\"
}"
} Conclusion from my opinion, the sub-json string value shouldn't be operated into multi-lines. and the '' ESC expected。However, I don't know the root reason. Besides, I'm introduce this project along with other third party project which is a tcp server, so I tested all this from a tcp client tool, pasting json strings above into client text widget as plain text, and send. So I guess the format reason is the most? Right? |
That's simply not valid JSON. Putting a backslash before a line break to escape it is not a thing in JSON (as far as I'm aware of). |
{
"some_key":"{
\"in_key\":\"in_value\"
}"
} Sorry for incomplete example list, I have added fourth example of incorrect list. I notice your point: not invalid JSON for ESC. But the fourth bad example should be a valid json? |
JSON doesn't support multi-line strings. Note that the Markdown renderer highlights your syntax errors in red. Below is the corrected version: {
"some_key":"{\n \"in_key\":\"in_value\"\n }"
} |
Thank you very much @falbrechtskirchinger . Now I understand what have happened. I will close this post, as it's not a bug. Sorry for wasting your time. |
Description
In some situation, I want a special json object which has a json string as its value, take following as a example:
set std::string
samplestring
with following value:Reproduction steps
Just pass the string above for parsing
Expected vs. actual results
Successfully parse that string with a json object, and we can also get sub-string of inner-json object.
Minimal code example
auto json = nlohmann::json::parse(samplestring);
Error messages
Compiler and operating system
ubuntu 18.04 g++ 7.5
Library version
Maybe 13? Don't get this project from GitHub, so don't know the version.
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: