Solved

JSON data From Census

Posted on 2013-11-01
4
467 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 52

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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 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 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 100 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now