Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Philip Powis
7,625 PointsHelp understanding how to parse and display api results
Hello - I've been struggling to understand how to go about parsing and displaying api results using html/js (or any other method that makes the best sense).
For Example:
Here is a very simply JSON results for codeivate(an awesome site if you haven't checked it out).
http://codeivate.com/users/ppowis.json
I'd like to understand how to take these results and simply display them back on an html page (maybe in table format). Nothing fancy. Just be able to consume the data, and read it back in an html page.
This seems like it should be pretty simple, but i'm really struggling to grasp the concept. Can anyone help provide a practical example of doing something similar, or even how you might approach the JSON I provided above?
Thanks!
2 Answers
Kevin Korte
28,149 PointsI know how to do it in PHP, and I'm playing with it in Jquery, if I figure that out I'll post a codepen link here.
In PHP, i would do it as so. I'd make a new php file, and I would post this:
$json_string = file_get_contents("http://codeivate.com/users/ppowis.json");
$parsed_json = json_decode($json_string);
$name = $parsed_json->{'name'};
$level = $parsed_json->{'level'};
And so on, for whatever pieces of info from that Json you needed. You could also write a loop here, which wouldn't be a bad idea. This Json isn't too long, but it isn't uncommon that you would use a loop instead to get the data. I intentionally left off the closing ?> php bracket on documents like this that are 100% php, since improper whitespace can screw with the doc, leaving of the closing tags will help insure you do not have that problem. Something I learned from Randy Hoyt here.
Than, on the php file that would contain the table or so, before I did the table, say at the very top of the page I would have.
<?php include('json.php');?>
Assuming that these two files are in the same directory. If not, you just need to adjust your url parameter to match your file structure.
Than just have my table, using my PHP variables to populate the values.
<table>
<thead>
<tr>
<th>Data</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td><?php echo $name;?></td>
</tr>
<tr>
<td>Level</td>
<td><?php echo $level;?></td>
</tr>
</tbody>
</table>
If you wrote a loop, than you'd just write another loop to create the table HTML and populate it with your loop of the JSON. Just depends on what you need. That's a way to do it server side anyway. This will work even if the user has Javascript off (only about 2% of users on average), but your server needs to have PHP installed (most do).
I'll work on a jquery version, I need to learn that anyway.
Kevin Korte
28,149 PointsHere is a jquery version. I've never done it this way before. There is much more that could be done, and I learned I need to learn more about this, but it seems like a good chance to try it out.
This isn't a production quality piece of code. It's to try something out. You'll notice I have absolutely no content in the HTML part, just empty divs.
The red box should turn to green when the program detects you coding. The message will also change too. You can change my if statements in the javascript to be not true (!=) if you want to see it change manually.
Have fun with it.