User Tools

Site Tools


en:public:developer:template_system:tags:api:json_response_format

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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: + &lt;rsp stat="ok"&gt
-jsonFlickrApi({ +   &lt;log&gt
- &quot;stat": "ok"+     &lt;revision&gt;7689&lt;/revision&gt
- &quot;blogs": { +     &lt;author&gt;nik&lt;/author&gt
- &quot;blog&quot;: [ +     &lt;date&gt;2/22/2013 1:10:45 PM&lt;/date&gt
- { +   &lt;/log&gt; 
- &quot;id&quot; &quot;73&quot;, +   &lt;log&gt
- &quot;name&quot; &quot;Bloxus test&quot;, +     &lt;revision&gt;7658&lt;/revision&gt
- &quot;needspassword&quot; : &quot;0&quot;, +     &lt;author&gt;www.arkadia.com&lt;/author&gt
- &quot;url&quot; &quot;http://remote.bloxus.com/&quot+     &lt;date&gt;2/21/2013 9:07:25 AM&lt;/date&gt
- }, +   &lt;/log&gt; 
-+ &lt;/rsp&gt;
- &quot;id&quot; &quot;74&quot;, +
- &quot;name&quot; &quot;Manila Test&quot;, +
- &quot;needspassword&quot; : &quot;1&quot;, +
- &quot;url&quot; &quot;http://flickrtest1.userland.com/&quot; +
-+
-+
-+
-})+
  
-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=0i&lt;rsp.blogs.blog.lengthi++){+  { 
 +   ";stat&quot;:"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 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({...});+
  
en/public/developer/template_system/tags/api/json_response_format.1362728711.txt.gz · Last modified: 2013/03/08 11:45 by admin