SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    SitePoint Enthusiast
    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.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't this "message" stored in a variable?
    mmj

  3. #3
    SitePoint Enthusiast
    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)

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vince_w View Post
    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)
    You are evaling in then, right?

    Code javascript:
    var data = eval(serverResponse);
    if (data.firstName)
    //
    if (data.lastName)
    //
     
    //etc...
    mmj

  5. #5
    SitePoint Addict ArunB's Avatar
    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);
    }

  6. #6
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by vince_w View Post
    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.
    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
    }
    I think, this will help you.

  7. #7
    SitePoint Member
    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];

  8. #8
    SitePoint Enthusiast
    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);
    }

  9. #9
    SitePoint Enthusiast
    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

  10. #10
    SitePoint Addict ArunB's Avatar
    Join Date
    Jun 2008
    Location
    Hyderabad
    Posts
    252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check, alert(assemby[key]); will work.

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Location
    Boston
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ArunB View Post
    Check, alert(assemby[key]); will work.
    thanks ArunB - i was actually wondering if this could be done without using a for-in loop.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •