Skip to content
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

Closed
himanshub16 opened this issue Oct 15, 2016 · 1 comment
Closed

How to handle json files? #333

himanshub16 opened this issue Oct 15, 2016 · 1 comment
Labels
kind: question solution: invalid the issue is not related to the library

Comments

@himanshub16
Copy link

himanshub16 commented Oct 15, 2016

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" } }

@himanshub16
Copy link
Author

himanshub16 commented Oct 15, 2016

Got it. My JSON files are syntactically wrong.
It should be :
{ "1" : { .. } , "2" : { ... } }

They should be strings. 😞

Sorry to bother you guys.

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Oct 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants