-
-
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 to handle json files? #333
Labels
Comments
Got it. My JSON files are syntactically wrong. They should be strings. 😞 Sorry to bother you guys. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried handling files, but it lead to runtime errors. Could you please help me figure out the bug?
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
int main()
{
ifstream file ("data.json", ios::in | ios::ate);
if (!file.is_open()) return 1;
streampos size = file.tellg();
file.seekg(0, ios::beg);
char *memblock = new char[size];
file.read(memblock, size);
file.close();
json data = json::parse(memblock);
// another approach
string s = memblock;
json data = json::parse(s);
// or
json data = json::parse(s.c_str());
...
All three methods to initialize json give runtime error :
terminate called after throwing an instance of 'std::invalid_argument' what(): parse error - unexpected number literal; expected string literal [1] 8754 abort (core dumped) ./a.out
The json file is
{ 1 : { "name": "himanshu" }, 2 : { "name":"shekhar" } }
The text was updated successfully, but these errors were encountered: