SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Seattle, WA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trouble getting nested JSON objects with JavaScript

    I'm trying to get the Store Latitude and Longitude values from the JSON below using JavaScript. JSONLint says I have valid JSON, and I'm able to get and use other parts of this JSON file (the "Charts" section, but not the "Maps" section). Not sure what I'm doing wrong here. Any ideas?

    Here's my JSON file:
    Code:
    {
         "Charts": {
              "Locations": [
    	       {
    	            "Views": 5,
    		    "Clicks": 0,
    		    "PhoneCalls": 0
    	        },
    		{
    		    "Views": 7,
    		    "Clicks": 1,
    	            "PhoneCalls": 0
    		}
               ]
         },
         "Maps": {
              "Locations": [
                   {
                        "ID0": {
                             "Store": {
    			      "Latitude": 59.327383,
    			      "Longitude": 18.06747
    			 },
    			 "Views": [
    			     {
    			          "Latitude": 59.32522,
    				  "Longitude": 18.07002
    			      },
    			      {
    				  "Latitude": 47.6097,
    				  "Longitude": 122.3331
    			      }
                             ]
                        },
                        "ID1": {
                             ...
                        }
                   }
              ]
         }
    }
    Obviously, I removed a lot of data so you could see the data I need clearly, but it is organized in this way.

    Here's my JS:
    Code:
    $jq.getJSON("/path/to/file.json", function(json){
         var jsonData = eval(json);
         var locationStoreLocation = new google.maps.LatLng(jsonData.Maps.Locations.ID0.Store.Latitude, jsonData.Maps.Locations.ID0.Store.Longitude);
    }
    The "new google.maps.LatLng()" is required for the Google JavaScript API I'm using. The error I get in my browser's javascript console is "Cannot read property 'Store' of undefined."

  2. #2
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,107
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    Hi,

    Maps.Locations contains an array so you'll need to access it with an index.
    Code javascript:
    id0 = jsonData.Maps.Locations[0].ID0;

  3. #3
    SitePoint Member
    Join Date
    Jan 2012
    Location
    Seattle, WA
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, of course! Thank you, markbrown4. It's working now.


Tags for this Thread

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
  •