Answer the question
In order to leave comments, you need to log in
How to return keys from an array?
Faced such a problem. We have JSONObject. We have the following array in it:
{
"12":"value1",
"27":"value2",
"85":"value3",
"34":"value4"
}
JSONObject jsonObject = new JSONObject("{"petr":"Петр","ivan":"Иван"}");
String[] jsonNames = jsonObject.getNames();
Answer the question
In order to leave comments, you need to log in
the question solved like this:
First put this somewhere:
private <T> Iterable<T> iteratorToIterable(final Iterator<T> iterator) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return iterator;
}
};
}
for (String key : iteratorToIterable(object.keys())) {
JSONObject entry = object.getJSONObject(key);
// ...
Cognitive numero uno fact: This data structure is not called an array.
En segundo, you can simply:
for (String key : jsonObject.keys()) {
String value = jsonObject.getString(key);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question