2017-01-25 1 views
-1

Est-il possible d'obtenir la liste des attributs d'une carte JSON particulière avec une expression JSONPath? Par exemple:Obtention d'une liste d'attributs dans un JSON

{"foo": 1, "bar": 2} =>["foo","bar"]

+0

Voici un [link] (http://stackoverflow.com/questions/208016/how-to-list-the-properties-of-a-javascript-object) qui pourrait vous aider. –

Répondre

0

boucle à travers chaque objet JSON obtenir toutes les clés et pousser ensuite dans un tableau. Vous pouvez essayer ceci:

var data=[{"foo": 1, "bar": 2},{"foo1": 1, "bar1": 2}]; 
 
var array=[]; 
 
$.each(data, function(key, value){ 
 
    var item; 
 
    item=Object.keys(value); 
 
    array.push(item); 
 
}); 
 

 
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

Vous pouvez utiliser le code suivant:

Object.getOwnPropertyNames({"foo": 1, "bar": 2}) 

Voici un link pour plus d'informations.