-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
How can I only parse the first level and get the value as string? #1175
Comments
The syntax error |
(The quote after the colon is invalid JSON.) |
I don't think it's the quote after the colon that's invalid, it's that there isn't enough quoting of the quotes inside the string, which is itself JSON. I think using a raw string literal would make this more obvious:
|
print pretty: {
"json": "{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.8054632351656703\",\"timestamp\": 1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
} get the respond with python or from your browser URL: http://recognition.bi.tuputech.com/v3/recognition >>> import requests
>>> r = requests.get('http://recognition.bi.tuputech.com/v3/recognition')
>>> respond = r.json()
>>> print json.dumps(respond, indent=4)
{
"json": "{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.11027675858112329\",\"timestamp\":1532617356385}",
"signature": "r5HFxSFJR7qb1eJqDYQUhc3KOMaFK6GO9zNIAp5ypAXxNfdjC6mbK4gRtCxhzHX6aETP6oJscZgRa8GNsN+WMmVEFxQQ9oxXLRMScyPXQKRXeiGgB8zOU+xDo263BEyGF6knLIjv+P/m9sEyrblwLIC21eLdOp6EoHVt0axZycI="
} It sounds reasonable if we treat the value of |
@kgbook If you put that text in a file, and read that, or wrap it in a raw string literal, it should work. If you just paste that in your code and put plain quotes around it, you have to quote each " character AND each \ character. |
@gregmarr I did, just see |
@kgbook You mean the one in your first message? That's wrong. You put it in your code, surrounded it in plain quotes, and quoted SOME of the characters. If you just use a regular string literal instead of a raw string literal, you need this:
|
@gregmarr oh, you are right! |
You want the resulting string to contain Raw string literals avoid all that, so you just wrap the string as-is in |
@gregmarr Great! I really appreciate your support! |
[json.exception.parse_error.101] parse error at 17: syntax error - invalid string: control character must be escaped; last read: '"{<U+000A>' environment:
kang:~ kang$ /Library/Developer/CommandLineTools/usr/bin/c++ -v
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin |
json in = "{\"json\": \"{\\\"error\\\":true,\\\"message\\\":\\\"404 not found\\\",\\\"code\\\":0,\\\"nonce\\\":\\\"0.8054632351656703\\\",\\\"timestamp\\\": 1532525070907}\",\"signature\": \"Q5s1tDJVXWcNXp\"}";
json in = R"({"json": "{\"error\": true, \"message\": \"404 not found\", \"code\": 0, \"nonce\": \"0.8054632351656703\", \"timestamp\": 1532525070907}", "signature": "Q5s1tDJVXWcNXp"})"_json;
json in = R"({
"json": "{
\"error\": true,
\"message\": \"404 not found\",
\"code\": 0,
\"nonce\": \"0.8054632351656703\",
\"timestamp\": 1532525070907
}",
"signature": "Q5s1tDJVXWcNXp"
})"_json; |
@kgbook Are you running on Windows? It is reporting a parse error at a carriage return character. |
Ah, I see above that you're not. I'm not sure why it doesn't like the newlines in the raw literal. |
@gregmarr Mac OS X, 10.13.5 (17F77) |
Three situation when using Raw string literals:
json in = R"({"json": "{\"error\": true, \"message\": \"404 not found\", \"code\": 0, \"nonce\": \"0.8054632351656703\", \"timestamp\": 1532525070907}", "signature": "Q5s1tDJVXWcNXp"})"_json;
json jStr = R"({
"json":"{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.8054632351656703\",\"timestamp\":1532525070907}",
"signature":"Q5s1tDJVXWcNXp"
})"_json;
json in = R"({
"json": "{
\"error\": true,
\"message\": \"404 not found\",
\"code\": 0,
\"nonce\": \"0.8054632351656703\",
\"timestamp\": 1532525070907
}",
"signature": "Q5s1tDJVXWcNXp"
})"_json; Only the last one |
Well, there are only two |
I can't agree that. json in = R"({
"json": "{\"error\": true,
\"message\": \"404 not found\",
\"code\": 0,
\"nonce\": \"0.8054632351656703\",
\"timestamp\": 1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
})"_json; Also |
@kgbook What is the exact issue you are having? As far as I can see, you seem to get parse error exceptions as you pass invalid JSON to the parser (which is implicitly called by the suffix |
@nlohmann The issue in my first message solved with the help of gregmarr. |
Which issue do you mean exactly? |
@kgbook Then the error must have changed, as before it was
|
@nlohmann yes, and more detailed information #1175 (comment) and #1175 (comment) |
Yes, the 3rd example is not valid JSON. |
@nlohmann Can you explain why 2 is valid but 3 is not? I've been trying to figure that out. |
@gregmarr Actually, the error info you reference is the latest. parse error at 12: syntax error - invalid literal; last read: '"{"e'; expected '}' [json.exception.parse_error.101] parse error at 17: syntax error - invalid string: control character must be escaped; last read: '"{<U+000A>' |
{
"json": "{\"error\": true, \"message\": \"404 not found\", \"code\": 0, \"nonce\": \"0.8054632351656703\", \"timestamp\": 1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
} {
"json": "{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.8054632351656703\",\"timestamp\":1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
}
after fixing the whitespace {
"json": "{ \"error\": true, \"message\": \"404 not found\", \"code\": 0, \"nonce\": \"0.8054632351656703\", \"timestamp\": 1532525070907 }",
"signature": "Q5s1tDJVXWcNXp"
} |
@kgbook So the parser is telling you where the parse error is. What is the issue then? |
@nlohmann the JSON data of the 3rd example is valid, but failed to |
U+000A is a Unicode Character named |
The error message says that that control character must be escaped. |
json jStr = R"({
"json": "{
\"error\": true,
\"message\": \"404 not found\",
\"code\": 0,
\"nonce\": \"0.8054632351656703\",
\"timestamp\": 1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
})"_json; It doesn't work. [json.exception.parse_error.101] parse error at 37: syntax error - invalid string: control character must be escaped; last read: '"{\n<U+000A>'` I modify the format, and there is a little difference. Break at nlohmann/json.hpp:7643, and print the value. (gdb) p s
$1 = 0x10002fa8f "{\n \"json\": \"{\n \\\"error\\\": true,\n \\\"message\\\": \\\"404 not found\\\",\n \\\"code\\\": 0,\n \\\"nonce\\\": \\\"0.8054632351656703\\\",\n \\\"timestamp\\\": 1532525070907}\",\n \"signature\": \"Q5s1tDJVXWcNXp\"\n }" However, the control character |
I guess when you just print the string to {
"json": "{
\"error\": true,
\"message\": \"404 not found\",
\"code\": 0,
\"nonce\": \"0.8054632351656703\",
\"timestamp\": 1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
} The error message indicates that a newline ( |
No, you perhaps misunderstand something. using json = nlohmann::json;
...
catch (json::parse_error &err){
cout <<"msg: " <<err.what() <<endl;
cout <<"exception id: " <<err.id <<endl;
cout <<"byte: " <<err.byte <<endl;
} so I got the error message from the You can test it and will get the same result. |
And I print the value of
|
I see no error in the parser. The string that you pass is not valid JSON. If you print the string to |
sorry, I can't open your URL. json jStr = R"({
"json":"{\"error\":true,\"message\":\"404 not found\",\"code\":0,\"nonce\":\"0.8054632351656703\",\"timestamp\":1532525070907}",
"signature":"Q5s1tDJVXWcNXp"
})"_json; json jStr = R"({
"json": "{
\"error\": true,
\"message\": \"404 not found\",
\"code\": 0,
\"nonce\": \"0.8054632351656703\",
\"timestamp\": 1532525070907}",
"signature": "Q5s1tDJVXWcNXp"
})"_json; the first is valid, and the other is not valid. In a word, the value of the key should't contain a |
Thanks |
I use the latest release version
3.1.2
.I want to parse the JSON data:
And My code:
main.cpp
main.h
Then I got a exception named
json.exception.parse_error
, the detail information isI just want to
decode
the value ofjson
object withstd::string
type, and then decode again.Note that:
There are not the same!
As for the first one, the value of
json
embrace with double quotes"
, and the other is not!So I have to decode the value of
json
as astring
, notobject
.I used RapidJson library but failed with the same issue, parseErrorCode:
kParseErrorObjectMissCommaOrCurlyBracket
.Is it possible to
decode
those json data I describe above with the nlomann JSON library?The text was updated successfully, but these errors were encountered: