I have the following JSON object:
"StudentData": {
"Name": "Mike",
"Age": 25,
"DateOfBirth": 9/25/1993,
"IsMarried": false
}
I'm working on a Javascript function that will present a dialog box to a user with the above information inside of it, however I'd like the information to be preceded with the object's name (i.e. "StudentData").
Let's say I have this object stored within a variable in my function, and let's call it myStudent.
I stumbled upon a post on SE using Object.keys(myStudent)[0] to get property name of the first key within StudentData, but I'm looking to extract the name of the object itself.
How may I go about doing this?
Thank you :)
EDIT:
I'm looking to get StudentData string, not any of the keys contained within the StudentData object itself.
obj.StudentData.Name. This is of course assuming as Kevin B says below, that you've usedJSON.parseand that it's valid json (the stuff you posted is not valid json - you're missing starting brackets andDateOfBirthisn't a string).StudentDataonce the json is converted to an object isn't in any way related to the object that it contains.myStudent? Can't do that. Are you looking to get the name of an object's keys? Looks like you already know how.