-
-
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
Error: out_of_range #1348
Comments
The problem seems to be that your input is invalid JSON. At least this is the output you mention. I am confused, because the title of this issue mentions The parse error should also contain a position in the code (since 3.4.0 with a line and column), and a detailed error message (like "unexepected end of file" or "invalid Unicode byte"). Can you please double check whether you can get the complete error message? You could also catch Furthermore, you could use tools like https://jsonlint.com to see where the parse error is. |
No I think that these problems are linked together. It's not like an "classic" error which appears in the Error-Section. While debugging it just stops the process, opens the "json_sax.hpp" file, and indicates an error in line 253 which says:
Thats why I mentioned "out_of_range" in the title of this issue. And it says, as I mentioned above:
I'm sorry if this is an stupid error or mistake but I'm pretty new to programming. |
The parser only throws an |
I tried different JSON files with different content but there is still the same error. {
"device": [
{
"command": "resetInstruments",
"statusCode": 0,
"wait": 1000
}
]
} So there isn't any large number but it still throws an Just to make sure, this is the c++ code I use for printing out the JSON Object: #include "pch.h"
#include "OwnUtil.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <Windows.h>
#include <stdio.h>
#include <SFML/Network.hpp>
#include <nlohmann/json.hpp>
#include <nlohmann/adl_serializer.hpp>
using json = nlohmann::json;
using namespace std;
int main()
{
string text;
json temp;
ifstream read;
read.open("response_resetinstruments.txt");
getline(read, text);
read.close();
json in = json::parse(text);
temp = in["/device/command/0"_json_pointer];
cout << temp;
```
I expected to get "resetInstruments" as an output in my console. |
Firstly, you can immediately parse from an std::ifstream read("response_resetinstruments.txt");
json in = json::parse(read); Secondly, JSON Pointer
To access "resetInstruments", the correct JSON Pointer would be |
Ok. I'm new to this so I want it as clear as possible. But still thank you for the improvement. However, I applied what you mentioned above, and there is still the same error. It points to the |
This code works for me: #include "json.hpp"
#include <iostream>
#include <fstream>
using json = nlohmann::json;
int main()
{
std::ifstream read("response_resetinstruments.txt");
json in = json::parse(read);
auto temp = in["/device/0/command"_json_pointer];
std::cout << temp << std::endl;
} and outputs
Can you try this code? |
Copied your code, inserted it in my programm and I still get the same error. I just had to include "pch.h" and the headerfiles are one folder above, so I have to include |
To rule out issues with the input file, can you try #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main()
{
json in = R"({ "device": [ { "command": "resetInstruments", "statusCode": 0, "wait": 1000 } ] })"_json;
auto temp = in["/device/0/command"_json_pointer];
std::cout << temp << std::endl;
} |
Sorry for not responting. |
I do not understand your questions. Can you please be more specific? |
I very appreciate that the code works if I directly store the String in the JSON variable. |
You can call |
Thank you so much. You solved my problem now I can go to the next step in my project. |
I'm trying to read in a .txt file which is packed with JSON Objects. Afterwards I want to get a specific object out of the document, but everytime I try to run my problem I get an Error-Message like:
(Sorry for bad english);
However this is just for testing purpose. Later I want to parse the return string of an function.
But for this problem my code looks like this:
`string text;
json temp;
and my .txt file with the JSON Objects looks like this (just a part of it because otherwise nobody would look at this post because it would be to long)
{"device":[{ "command":"enumerate", "statusCode":0, "wait":0, "deviceMake":"Digilent", "deviceModel":"OpenScope MZ", "firmwareVersion":{ "major":1, "minor":301, "patch":0}, "macAddress":"00:7E:G0:8B:6C:CC", "calibrationSource":"flash", "nics":{ "wlan0":{ "macAddress":"00:1E:B0:8B:5B:AC"}}, "requiredCalibrationVer":17, "requiredWiFiParameterVer":8, "awg":{ "numChans":1,
The text was updated successfully, but these errors were encountered: