This question of yours is very similar to this another you posted.
Consider checking the package class documentation org.json
, chiefly JSONObject
and JSONArray
. Still see if this answer below helps you.
Again, let us consider your JSON, reproduced below:
{
"Ficha":[
{
"nome":"nome",
"sobrenome":"sobrenome",
"idade":"idade",
"endereco":"endereco",
"empresa":"empresa",
"telefones":[
{
"residencial":"residencial"
},
{
"celular":"celular"
}
]
}
]
}
We have an unnamed object that contains a array of Ficha
, then we will build the reproduction of this JSON as a JSONObject
, in this way:
final JSONObject json = new JSONObject(json);
After this we will retrieve the array of Ficha
, as follows:
final JSONArray fichas = json.getJSONArray("Ficha");
If we print the contents of fichas
using fichas.toString(2)
we will have the following exit:
[{
"idade": "idade",
"endereco": "endereco",
"nome": "nome",
"sobrenome": "sobrenome",
"empresa": "empresa",
"telefones": [
{"residencial": "residencial"},
{"celular": "celular"}
]
}]
If you want the array of telefones
you will have before you recover the JSONObject
of Ficha
and then the JSONArray
of telefones
, something like this:
final JSONObject ficha = fichas.getJSONObject(i);
final JSONArray telefones = ficha.getJSONArray("telefones");
By printing the contents of telefones
using telefones.toString(2)
we will have the following exit:
[
{"residencial": "residencial"},
{"celular": "celular"}
]
This is a complete example that generated the outputs shown:
final JSONObject json = new JSONObject(getJSON());
final JSONArray fichas = json.getJSONArray("Ficha");
System.out.println(fichas.toString(2));
final int size = fichas.length();
for (int i = 0; i < size; i++) {
final JSONObject ficha = fichas.getJSONObject(i);
final JSONArray telefones = ficha.getJSONArray("telefones");
System.out.println(telefones.toString(2));
}
EDITION
To recover only the existing element names you can use the methods names()
or keys()
An example using names()
is this:
final JSONArray names = json.names();
final int nSize = names.length();
for (int i = 0; i < nSize; i++) {
final Object name = names.get(i);
System.out.println(name);
}
That will result in this exit:
Token
Already one using keys()
is this:
final Iterator<String> iKeysIterator = json.keys();
while (iKeysIterator.hasNext()) {
System.out.println(iKeysIterator.next());
}
That will result in the same exit:
Token
See if the method is available keySet()
also, if you are, you can use it as follows:
final Set<String> keys = json.keySet();
for (final String key : keys) {
System.out.println(key);
}
so that other answer from you really helped, I was able to get the contents of the array from both the plug and the phone, but now I need to get the name of the array, would I have to name the array "Plug" in json?
– daniel12345smith
@daniel12345smith has how to update your reply with the content you expect? You want to recover the literal Token and telephones, is that it? I don’t understand...
– Bruno César
i would like to retrieve only the name of the "Token" array and use that name to put inside a textView, because I am making a dynamic form. the "phones" I can pick up because it is inside the plug array, but what about the "Plug" which is the main name? it must be a simple thing, is that I’m picking up here.
– daniel12345smith
@daniel12345smith updated reply, see if this is it
– Bruno César
Dude thanks, I’ll test.
– daniel12345smith
I’ll try with JS even, I was already trying to solve this with JS.
– daniel12345smith