This shows you the differences between two versions of the page.
en:public:developer:template_system:tags:api:json_response_format [2013/03/08 11:45] admin |
en:public:developer:template_system:tags:api:json_response_format [2013/03/08 12:48] (current) admin [Callback Function] |
||
---|---|---|---|
Line 9: | Line 9: | ||
request with a value of "json". | request with a value of "json". | ||
- | ==== Object Representation ==== | + | ===== Object Representation ===== |
Some simple rules are used when converting REST XML into JSON objects. | Some simple rules are used when converting REST XML into JSON objects. | ||
Line 59: | Line 59: | ||
{ | { | ||
"outer": { | "outer": { | ||
- | "photo": [ | + | "photos": [ |
- | {"id": "1"}, | + | {"id": "1"}, |
- | {"id": "2"} | + | {"id": "2"} |
- | ] | + | ] |
} | } | ||
} | } | ||
</code> | </code> | ||
- | ==== Successful Responses ==== | + | ===== Successful Responses ===== |
When a request is successful, the following JSON is returned: | When a request is successful, the following JSON is returned: | ||
- | {...} | + | {stat":"ok",...} |
The single object passed in the function call represents the REST outer <rsp> element. | The single object passed in the function call represents the REST outer <rsp> element. | ||
The root element of the REST response is then a member of this object (the stat param also is - | The root element of the REST response is then a member of this object (the stat param also is - | ||
- | check this to see if the call was successful). For instance, flickr.blogs.getList has a | + | check this to see if the call was successful). For instance, get_wwwfile_logs has a |
documented response as follows: | documented response as follows: | ||
- | <blogs> | + | <code xml> |
- | <blog id="73" name="Bloxus test" needspassword="0" | + | |
- | url="http://remote.bloxus.com/" /> | + | |
- | <blog id="74" name="Manila Test" needspassword="1" | + | |
- | url="http://flickrtest1.userland.com/" /> | + | |
- | </blogs> | + | |
- | And the Flickr JSON equivilent would be: | + | <rsp stat="ok"> |
- | jsonFlickrApi({ | + | <log> |
- | "stat": "ok", | + | <revision>7689</revision> |
- | "blogs": { | + | <author>nik</author> |
- | "blog": [ | + | <date>2/22/2013 1:10:45 PM</date> |
- | { | + | </log> |
- | "id" : "73", | + | <log> |
- | "name" : "Bloxus test", | + | <revision>7658</revision> |
- | "needspassword" : "0", | + | <author>www.arkadia.com</author> |
- | "url" : "http://remote.bloxus.com/" | + | <date>2/21/2013 9:07:25 AM</date> |
- | }, | + | </log> |
- | { | + | </rsp> |
- | "id" : "74", | + | |
- | "name" : "Manila Test", | + | |
- | "needspassword" : "1", | + | |
- | "url" : "http://flickrtest1.userland.com/" | + | |
- | } | + | |
- | ] | + | |
- | } | + | |
- | }) | + | |
- | In JavaScript, displaying a list of a user's blogs is then straight forward: | + | </code> |
- | function jsonFlickrApi(rsp){ | + | |
- | if (rsp.stat != "ok"){ | + | And the JSON equivilent would be: |
- | // something broke! | + | <code javascript> |
- | return; | + | |
- | } | + | |
- | for (var i=0; i<rsp.blogs.blog.length; i++){ | + | { |
+ | "stat":"ok", | ||
+ | "logs":[ | ||
+ | { | ||
+ | "revision":"7689", | ||
+ | "author":"nik", | ||
+ | "date":"2/22/2013 1:10:45 PM" | ||
+ | }, | ||
+ | { | ||
+ | "revision":"7658", | ||
+ | "author":"www.arkadia.com", | ||
+ | "date":"2/21/2013 9:07:25 AM" | ||
+ | } | ||
+ | ] | ||
+ | } | ||
- | var blog = rsp.blogs.blog[i]; | + | </code> |
- | var div = document.createElement('div'); | + | In JavaScript, displaying the logs is then straight forward: |
- | var txt = document.createTextNode(blog.name); | + | |
- | div.appendChild(txt); | + | <code javascript> |
- | document.body.appendChild(div); | + | |
- | } | + | function showLogs(rsp){ |
- | } | + | |
+ | if (rsp.stat != "ok"){ | ||
+ | // something broke! | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | for (var i=0; i<rsp.logs.length; i++){ | ||
+ | var div = document.createElement('div'); | ||
+ | var txt = document.createTextNode(rsp.logs[i].author); | ||
+ | div.appendChild(txt); | ||
+ | document.body.appendChild(div); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </code> | ||
===== Failure Responses ===== | ===== Failure Responses ===== | ||
- | Failure responses also call the jsonFlickrApi() method, but with a different JSON object. The object is not structured like the REST failure responses - instead it's simplified for JSON. For example: | + | Failure responses will return a different JSON object. |
- | jsonFlickrApi({ | + | The object is not structured like the REST failure responses - |
- | "stat" : "fail", | + | instead it's simplified for JSON. For example: |
- | "code" : "97", | + | |
- | "message" : "Missing signature" | + | <code javascript> |
- | }) | + | { |
+ | "stat": "fail", | ||
+ | "code": "97", | ||
+ | "message": "Error message" | ||
+ | } | ||
+ | </code> | ||
- | From JavaScript, you can check rsp.stat for failure, and then read the error from rsp.code and rsp.message. | + | From JavaScript, you can check rsp.stat for failure, and then read the |
+ | error from rsp.code and rsp.message. | ||
===== Callback Function ===== | ===== Callback Function ===== | ||
- | If you just want the raw JSON, with no function wrapper, add the parameter nojsoncallback with a value of 1 to your request. | + | To define your own callback function name, add the parameter **jsoncallback** with your desired name as |
+ | the value. | ||
- | To define your own callback function name, add the parameter jsoncallback with your desired name as the value. | + | no jsoncallback -> {"stat":"ok",...}\\ |
- | nojsoncallback=1 -> {...} | + | jsoncallback=wooYay -> wooYay({"stat":"ok",...}); |
- | jsoncallback=wooYay -> wooYay({...}); | + | |