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

Creating an empty array #27

Closed
edi9999 opened this issue Jan 13, 2015 · 3 comments
Closed

Creating an empty array #27

edi9999 opened this issue Jan 13, 2015 · 3 comments

Comments

@edi9999
Copy link

edi9999 commented Jan 13, 2015

I'm using the following code right now:

json customers;

for (...)
{
  customers.push_back(...)
}

cout << customers;

However, if the for loop doesnt happen, I would like to return an empty array (eg [])

Is they an other way to get this behaviour without doing json customers= json::parse("[]"); ?

@Teemperor
Copy link
Contributor

I assume this is fixed by fixing #8

@nlohmann
Copy link
Owner

@Teemperor is right - this shall be fixed with issue #8. I hope to do this by tomorrow.

So far, you could try

json customers(json::value_type::array);

to tell the class to create an empty JSON array.

nlohmann added a commit that referenced this issue Jan 24, 2015
@nlohmann nlohmann self-assigned this Jan 24, 2015
@nlohmann
Copy link
Owner

There now is a function json::array() to create an empty array. It can also be used with an initializer list (e.g., json::array({"foo", "bar"})) to explicitly create the array ["foo", "bar"] rather than the object {"foo": "bar"}.

GerHobbelt pushed a commit to GerHobbelt/nlohmann-json that referenced this issue May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants