How to get a JSON object from a RESTful api and parse it?
Now one of my friends designed a set of RESTful APIs. Some of them are 'GET' method and return JSON format data.
And I have to write a html page with some javascript code to get data from his API and parse the data.
When I enter a text and click a button, it will send a GET request to my friend's API server. How to send this request and get this json data from this api and store it in a javascript variable?.
And the format of this json data is like that:
[ { key1 : "value1", key2 : "value2", key3 : "value3", key4 : "value4" }, { key1 : "value1", key2 : "value2", key3 : "value3", key4 : "value4" } ]
How to display it on html page in a table with the first row is the name of all keys? And the other rows is all the values?
And can set one value like value4 as a hyperlink where I can click? When I click it, it will create a new page and get something new from the API server?
Answers
This is somehow a very long to answer question. I will give you some directions.
take a look at jQuery.ajax() method if you are using jQuery. If not, I recommend you do that. site is: http://wwww.jquery.com
That will help you make the request and get the data back into a Javascript Object.
Then you can update the DOM which also jQuery is very useful to update or create a table with the data.
Either way, I recommend to check out some MVVM libraries to ease some of the work like Knockout.js or Angular.js
Depending on your skills this might be a little bit 'hard' to get to know, so you can learn jQuery first and manipulate the DOM using it so you can achieve the result you want.