Browse Jsonobject Android

Asked

Viewed 480 times

1

How to go through an Object array in the way q I can get the data?

    {"number1":"value1", "number2":"value2", "number3":"value3"} 

  for (int c = 0; c < jsonObject.length(); c++) {

  }

1 answer

2


Well, actually that figure is a Jsonobject. A Object Array would be something like:

Object[] arr = new Object[];
List<Object> list = new ArrayList<Object>;
/* etc */

Back to the subject, you can capture the keys. This catch will return you a Iterator.

With that Iterator you can go through all the keys and thus capture the values through a while or do..while

JSONObject j = new JSONObject("{\"number1\":\"value1\", \"number2\":\"value2\", \"number3\":\"value3\"} ");
Iterator<String> keys = j.keys();

// Verifica se há mais alguma key
while (keys.hasNext() {
    // Captura a key e seu valor; e avança para a próxima
    System.out.println( j.get( keys.next() ).toString() );
}

Or you can use the for

JSONObject j = new JSONObject("{\"number1\":\"value1\", \"number2\":\"value2\", \"number3\":\"value3\"} ");

for ( Iterator<String> Keys = j.keys(); Keys.hasNext(); ) {
    System.out.println( j.get( Keys.next() ).toString() );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.