Link to home
Start Free TrialLog in
Avatar of lvmllc
lvmllcFlag for United States of America

asked on

JSON data From Census

I am working with the US Census API and am curious if I am doing this right or if there is a better way to do this.
I am using CORS but could do this with jsonp

The urls and output are

http://api.census.gov/data/2010/sf1?key=...&get=P0010001,NAME&for=state:19,38

[["P0010001","NAME","state"],
["3046355","Iowa","19"],
["672591","North Dakota","38"]]

http://api.census.gov/data/2010/sf1?key=...&get=P0010001,NAME&for=state:19,38&jsonp=cjs

cjs([["P0010001","NAME","state"],
["3046355","Iowa","19"],
["672591","North Dakota","38"]])


In the case of the first URL I have the following code

 $.getJSON(url,function(data,status){
      alert(data);
      console.log(data[1][1])
    });

Open in new window


The alert produces a string
P0010001,NAME,state,3046355,Iowa,19,672591,North Dakota,38

and the console prints Iowa

My first question is why does Alert make a string and not display object object?

I think I am OK using this jquery as I am able to access the values, but would like to know if any of you experts have concerns about this method.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

The reason is your alert is asking for all the data and what you are sending to the console is the 2nd item in the 2nd row of data.  Array's are zero based.  If you used 0,0 instead, you would have seen, P0010001
Avatar of lvmllc

ASKER

I understand the 0,0 base. I was just surprised that an alert without specifying the array location returns the full data string.

Any comments on if this is the best approach to grabbing this JSON data?
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial