append
only makes sense for arrayValue
, that is list-type JSON values.
When jsoncpp creates a value, by default, it starts as a nullValue
. If you then perform array-style operations on it like append
, it'll turn it into an arrayValue
and carry on. This is what happens when you write
Json::Value root; // Root starts as a nullValue
root.append(val); // Root becomes an arrayValue
// and from now on array-type behaviors are okay
But that doesn't work in your first code snippet. Because similarly to append
causing a conversion from nullValue
to arrayValue
, using operator[]
with string keys to do assignment converts it into an objectValue
, a collection of key-value pairs. (operator[]
with integer keys would have turned it into an arrayValue
, since in a JSON object keys must be strings.)
Json::Value val; // val starts as a nullValue
val["1"] = 1; // val becomes an objectValue
// and from now on dict-style operations are okay
val["2"] = 2;
...
val.append(arr); // ERROR: val only accepts dict-style operations
To see why a dict-style Value
can't accept append
, ask yourself what key should be associated with the value you're append
ing.