How to get the name of the properties of a Javascript or JSON object?

Asked

Viewed 6,738 times

-1

{
   "Ficha":[
      {
         "nome":"nome",
         "sobrenome":"sobrenome",
         "idade":"idade",
         "endereco":"endereco",
         "empresa":"empresa",
         "telefones":[
            {
               "residencial":"residencial"
            },
            {
               "celular":"celular"
            }
         ]
      }
   ]
}

I wanted to get the "Plug" but when I put one getString("Ficha") returns the full array.

How do I only take the string "Plug" and then only take the string "phones"?

2 answers

2

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 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...

  • 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 updated reply, see if this is it

  • Dude thanks, I’ll test.

  • I’ll try with JS even, I was already trying to solve this with JS.

Show 1 more comment

0


You’re trying to get the name of the property of the object, you can do something like this:

var meuObjeto = {"carro": "civic", "casa": "fazenda"};
Object.keys(meuObjeto);

this will print

["carro", "casa"]

Works on IE 9 or higher.

Or make a for getting the keys

for(var k in meuObjeto){
   console.log(k);
} 

Works on IE 7 or higher

In his example:

var json = {
   "Ficha":[
      {
         "nome":"nome",
         "sobrenome":"sobrenome",
         "idade":"idade",
         "endereco":"endereco",
         "empresa":"empresa",
         "telefones":[
            {
               "residencial":"residencial"
            },
            {
               "celular":"celular"
            }
         ]
      }
   ]
};


 console.log(Object.keys(json));

prints

["Ficha"] 

Browser other questions tagged

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