SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
Jun 29, 2008, 09:08 #1
- Join Date
- Feb 2008
- Location
- Boston
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
how to find out what keys are in a json message
anyone know if it's possible to find out what keys are present in a json message?
for example, if the json message is
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
"212 555-1234",
"646 555-4567"
]
}
i need to get firstName, lastName, address, and phoneNumbers.
-
Jun 29, 2008, 10:14 #2
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Isn't this "message" stored in a variable?
mmj
-
Jun 29, 2008, 23:32 #3
- Join Date
- Feb 2008
- Location
- Boston
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi mmj. can you elaborate? i'm not sure what that means.
(the JSON data/message is sent from the server back to the browser that requests it)
-
Jun 29, 2008, 23:36 #4
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
try
for(key in <<JSON object name>>) {
alert(key);
}
-
Jun 30, 2008, 03:29 #5
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 30, 2008, 03:39 #6
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If this is response from Server, use eval() function on client side. But this eval() function is not suggestible. Use JSON.parse (Download JSON.js from http://www.json.org/).
If it (the code you are displaying) is a object (already evaluated or is on client side, not received from server) then to find keys in this JSON object, use
Code:for(key in <<JSON Object>>) { alert(key); //key is the key in JSON object }
-
Jun 30, 2008, 10:46 #7
- Join Date
- Jun 2008
- Posts
- 4
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
These guys are right, if you don't know what your keys are, you need to loop through the object (with a for in loop) and grab the keys in this manner.
Prototype JS also provides an Object.keys() method that will return an array of the keys.
When you say "i need to get firstName, lastName, address, and phoneNumbers.", it almost sounds to me like you are trying to access the values in the object. Here's how you do so.
If your Object is defined as,
var userInfo = {"firstName": "John", "lastName": "Smith", "address": {"streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 0021}, "phoneNumbers": ["212 555-1234","646 555-4567"]};
You can access these values via their keys.
var firstName = userInfo.firstName;
var lastName = userInfo.lastName;
var address = userInfo.address;
var phone1 = userInfo.phoneNumbers[0];
var phone2 = userInfo.phoneNumbers[1];
-
Jun 30, 2008, 15:35 #8
- Join Date
- Feb 2008
- Location
- Boston
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks guys. that works! this is what i ended up doing:
Code:function listMarkers(url) { var request = google.maps.XmlHttp.create(); request.onreadystatechange = function() { if (request.readyState == 4) { var assemblies = eval( request.responseText ); for (var i = 0 ; i < assemblies.length ; i++) { var assembly = assemblies[i] for(key in assembly) { alert(key); } } } } request.open('GET', url, true); request.send(null); }
-
Jun 30, 2008, 21:52 #9
- Join Date
- Feb 2008
- Location
- Boston
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
out of curiosity, anyone know if it's possible to get the keys without using a for in loop?
for example, instead of
for(key in assembly) {
alert(key);
}
something like
alert(assembly[0]); //doesn't work
alert(assemby.key); //doesn't work
-
Jul 1, 2008, 00:44 #10
- Join Date
- Jun 2008
- Location
- Hyderabad
- Posts
- 252
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Check, alert(assemby[key]); will work.
-
Jul 1, 2008, 08:26 #11
- Join Date
- Feb 2008
- Location
- Boston
- Posts
- 38
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jul 1, 2008, 13:30 #12
- Join Date
- Dec 2007
- Posts
- 207
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks