?
Solved

JSON data From Census

Posted on 2013-11-01
4
Medium Priority
?
510 Views
Last Modified: 2013-11-02
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.
0
Comment
Question by:lvmllc
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Scott Fell, EE MVE
ID: 39617898
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
0
 

Author Comment

by:lvmllc
ID: 39618207
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?
0
 
LVL 55

Accepted Solution

by:
Scott Fell,  EE MVE earned 1000 total points
ID: 39618238
If your "get" or "post" returns 1 or 10,000 records, they all would show up when you try and output "data".     From there, you would not typically use "data" to display your the output unless for instance the data being returned was not json and instead a short bit of text or  html.  If you do have 10,000 rows of data in json/jsonp it is all on the client machine and you can now access the data many multiple times by calling just the portion of data you need without making more trips to the server.  

10,000 rows of data is more than likely going to be too much and there would be a long initial lag time for all that data to load.   Let's say you do have 10,000 total rows of data, you would probably want to make your ajax call return just several hundred rows at a time.  Then on your actual web page, you may display just 10 rows of data at a time.  You would set up some type of paging that tracks where the user is and at the appropriate spot, call additional data to load similar to this example http://datatables.net/release-datatables/examples/server_side/jsonp.html
0
 
LVL 46

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 400 total points
ID: 39618803
My first question is why does Alert make a string and not display object object?

Because your returned data is an array - not an object!
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

585 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question