Json: Creating an empty array

Created on 13 Jan 2015  路  3Comments  路  Source: nlohmann/json

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("[]"); ?

enhancemenimprovement

Most helpful comment

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

All 3 comments

I assume this is fixed by fixing #8

@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.

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

Was this page helpful?
0 / 5 - 0 ratings