JSON example: jQuery parses JSON data

Source: Internet
Author: User

JSON is a more convenient data in the form of specific you can refer to http://baike.baidu.com/view/136475.htm is quite detailed.

Today, I wrote a JSON object using JQuery and used the $. getJSON method to obtain JSON data and parse it easily.

From http://api.flickr.com/services/feeds/photos_public.gne? Tags = cat & tagmode = any & format = json & jsoncallback =? This address obtains JSON data, analyzes the structure, and generates images and related links:

JS:

 
 
  1. $ (Function (){
  2. Var url = "http://api.flickr.com/services/feeds/photos_public.gne? Tags = cat & tagmode = any & format = json & jsoncallback =? "
  3. $. GetJSON (// use the getJSON method to retrieve JSON data
  4. Url,
  5. Function (data) {// processing data points to the returned JSON data
  6. Var tit = "<a href = '" + data. link + "'>" + data. title + "<\/a>"; // generate a title and title connection
  7. $ ("H1" 2.16.html (tit); // appears in the specified position H1
  8. $ ("# Ginfo"). find ("p" ).eq(0).html (data. modified );
  9. $ ("# Ginfo"). find ("p" ).eq(1).html (data. generator );
  10. Var lis = ""; // li list project
  11. $ (Data. items). each (function (I, ite) {// retrieve JSON data to obtain the required form
  12. Lis + = "<li> ";
  13. Lis + = "<a href = '" + ite. link + "'> <\/a> ";
  14. Lis + = "<div> ";
  15. Lis + = ite. description;
  16. Lis + = "<\/div> <\/li> ";
  17. })
  18. $ ("Ul" ).html (lis); // present the retrieved data to the desired location
  19. $ ("Li "). hover (function () {$ (this ). addClass ("hov")}, function () {$ (this ). removeClass ("hov ")});
  20. }
  21. )
  22. })

HTML:

 
 
  1. <div class="main"> 
  2.     <div class="ginfo" id="ginfo">      
  3.         
  4.         <p></p> 
  5.         <p></p> 
  6.    </div> 
  7.    <ul>         
  8.    </ul> 
  9. </div>    

Finally let's talk about the JSON data format is actually a text file, you can easily parse can also directly view the http://api.flickr.com/services/feeds/photos_public.gne? Tags = cat & tagmode = any & format = json & jsoncallback =? This address.

 
 
  1. ({        
  2.  "title": "Recent Uploads tagged cat",         
  3.  "link": "http://www.flickr.com/photos/tags/cat/",        "description": "",    
  4.       "modified": "2009-08-03T01:50:45Z",         
  5.  "generator": "http://www.flickr.com/",         
  6.  "items"    
  7.      {            
  8.   "title": "DSC06844",         
  9.      "link": "http://www.flickr.com/photos/g_bugel/3783605340/",              
  10. "media": {"m":"http://farm3.static.flickr.com/2638/3783605340_a3cfc9eeb9_m.jpg"},             
  11.  "date_taken": "2009-07-06T07:27:59-08:00",            
  12.  "description": "<p><a href=\"http://www.flickr.com/people/g_bugel/\">g.bugel<\/a> posted a photo:<\/p> <p><a href=\"http://www.flickr.com/photos/g_bugel/3783605340/\" title=\"DSC06844\"><\/a><\/p> ",           
  13.    "published": "2009-08-03T01:50:45Z",           
  14.    "author": "nobody@flickr.com (g.bugel)",           
  15.    "author_id": "38658309@N00",             
  16.    "tags": "china cat feline beijing 2009 chinalab chinalab2009"        
  17.  },{......}); 

Click here to view the DEMO

Link: http://www.cnblogs.com/pifoo/archive/2011/05/21/Json-Example.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.