JSON
JSONArrayIterator
// EXAMPLE
JSONValue outerObj = JSONValue::parseCopy(
"[\"abc\",\"def\",\"xxx\"]");
JSONArrayIterator iter(outerObj);
for(size_t ii = 0; iter.next(); ii++) {
Log.info("%u: %s", ii, iter.value().toString().data());
}
// OUTPUT
0000004723 [app] INFO: 0: abc
0000004723 [app] INFO: 1: def
0000004723 [app] INFO: 2: xxx
In order to access array elements, you must create a JSONArrayIterator
object from a JSONValue
.
The basic flow is:
- Create a
JSONArrayIterator
from aJSONValue
. - Call
iter.next()
. This must be done before accessing the first value, and to access each subsequent value. When it returns false, there are no more elements. It's possible fornext()
return false on the first call for an empty object. - Use
iter.name()
anditer.value()
to get the key name and value.
There is no method to find a value from its index; you must iterate the array to find it.
JSONArrayIterator::JSONArrayIterator(const JSONValue &value)
// PROTOTYPE
explicit JSONArrayIterator(const JSONValue &value);
Construct an array iterator.
JSONArrayIterator::next()
// PROTOTYPE
bool next();
Call next()
before accessing value()
. Then each subsequent call will get the value. When it returns false, there are no elements left. It's possible for next()
to return false on the first call if the array is empty.
There is no rewind function to go back to the beginning. To start over, construct a new JSONArrayIterator
from the JSONValue
again.
JSONArrayIterator::value()
// PROTOTYPE
JSONValue value() const;
Returns the current value in the array.
You will typically use methods of JSONValue
like toInt()
, toBool()
, toDouble()
, and toString()
to get useful values. For example:
iter.value().toInt()
: Returns a signed 32-bit int.iter.value().toDouble()
: Returns a 64-bit double.iter.value().toString().data()
: Returns aconst char *
. You can assign this to aString
to copy the value, if desired.
JSONArrayIterator::count()
// PROTOTYPE
size_t count() const;
Returns the count of the number of remaining values. As you call next()
this value will decrease.