Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
108
rated 0 times [  113] [ 5]  / answers: 1 / hits: 26715  / 12 Years ago, mon, december 17, 2012, 12:00:00

Preamble: I'm Italian, sorry for my bad English.



I need to retrieve the name of the property from a json object using javascript/jquery.



for example, starting from this object:



{
Table: {
Name: Chris,
Surname: McDonald
}
}


is there a way to get the strings Name and Surname?



something like:



//not working code, just for example
var jsonobj = eval('(' + previouscode + ')');
var prop = jsonobj.Table[0].getPropertyName();
var prop2 = jsonobj.Table[1].getPropertyName();
return prop + '-' + prop2; // this will return 'Name-Surname'

More From » jquery

 Answers
9
var names = [];
for ( var o in jsonobj.Table ) {
names.push( o ); // the property name
}


In modern browsers:



var names = Object.keys( jsonobj.Table );

[#81382] Friday, December 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;