How do I get the name of the properties of a JavaScript or JSON Object?

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

I wanted to get the "Tab" but when I put a getString("Ficha") returns the complete array.

How do I get just the string "Tab" and then just pick up the String "phones"?

    
asked by anonymous 19.06.2015 / 20:20

2 answers

-1

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

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

this will print

["carro", "casa"]

It works in IE 9 or higher.

Or make a for by picking up the keys

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

It works in IE 7 or higher

In your 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"] 
    
19.06.2015 / 22:51
2

This question of yours is very similar to this is another you posted.

Consider verifying the packages class documentation org.json , mainly JSONObject and JSONArray . Even so, see if this answer below will help.

Again, let's 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 , so we will construct the reproduction of this JSON as a JSONObject , like this:

final JSONObject json = new JSONObject(json);

After this we will recover the array from 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 output:

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

If you also want the array of telefones you will have to recover JSONObject Ficha and then JSONArray telefones , something like this:

final JSONObject ficha = fichas.getJSONObject(i);
final JSONArray telefones = ficha.getJSONArray("telefones");

When we print the contents of telefones using telefones.toString(2) we will have the following output:

[
  {"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 retrieve only 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 output:

  

Tab

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 output:

  

Tab

See if the keySet() method is available also, if it is, you can use it as follows:

final Set<String> keys = json.keySet();
for (final String key : keys) {
    System.out.println(key);
}
    
19.06.2015 / 21:44