• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Jquery And Json parser ...

Here you will know  how to parse JSON with jQuery for any project, Here is the JSON object that we will be parsing:

   1. {attributes:[{title: "Project Delivered", post_id: 31, post_type_id: 5, parent_post_id: null, description: "Working", time_zone: "International Date Line West", user_id: 4, image_path: "http://localhost:3000/user/image_path/4/thumb/3.jpg", user_name: "TestUser1", created_at: "Thu Mar 27 04:29:33 UTC 2008", email: "testuser1@testurl.com"},{title: "Task Response", post_id: 45, post_type_id: 6, parent_post_id: null, description: "completed", time_zone: "International Date Line West", user_id: 3, image_path: "http://localhost:3000/user/image_path/3/thumb/2.jpg", user_name: "TestUser2", created_at: "Fri Mar 28 06:13:49 UTC 2008", email: "testuser2@testurl.com"},{title: "abhijats post", post_id: 58, post_type_id: 1, parent_post_id: 55, description: "", time_zone: "International Date Line West", user_id: 2, image_path: "http://localhost:3000/user/image_path/2/thumb/9.jpg", user_name: "TestUser3", created_at: "Fri Mar 28 11:37:16 UTC 2008", email: "testuser3@testurl.com"},{title: "Give demo", post_id: 41, post_type_id: 4, parent_post_id: null, description: "74strong76Give demo74/strong76", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 06:04:02 UTC 2008", email: "testuser4@testurl.com"},{title: "Status Update", post_id: 37, post_type_id: 5, parent_post_id: null, description: "Working", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 05:20:37 UTC 2008", email: "testuser4@testurl.com"}, 
   2.  
   3. {title: "abhijat has read acess", post_id: 57, post_type_id: 1, parent_post_id: 55, description: "", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "AbhilashKumar", created_at: "Fri Mar 28 11:16:50 UTC 2008", email: "testuser4@testurl.com"}, 
   4.  
   5. {title: "Inxero", post_id: 48, post_type_id: 2, parent_post_id: null, description: "Inxero", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 06:56:28 UTC 2008", email: "testuser4@testurl.com"}, 
   6.  
   7. {title: "New public porjet", post_id: 50, post_type_id: 2, parent_post_id: null, description: "", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 10:30:04 UTC 2008", email: "testuser4@testurl.com"}, 
   8.  
   9. {title: "Status Update", post_id: 35, post_type_id: 5, parent_post_id: null, description: "Hey the stuff is coming good", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 01:50:48 UTC 2008", email: "testuser4@testurl.com"}, 
  10.  
  11. {title: "Status Update", post_id: 36, post_type_id: 5, parent_post_id: null, description: "kljklkl", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 01:59:08 UTC 2008", email: "testuser4@testurl.com"}]} 

{attributes:[{title: "Project Delivered", post_id: 31, post_type_id: 5, parent_post_id: null, description: "Working", time_zone: "International Date Line West", user_id: 4, image_path: "http://localhost:3000/user/image_path/4/thumb/3.jpg", user_name: "TestUser1", created_at: "Thu Mar 27 04:29:33 UTC 2008", email: "testuser1@testurl.com"},{title: "Task Response", post_id: 45, post_type_id: 6, parent_post_id: null, description: "completed", time_zone: "International Date Line West", user_id: 3, image_path: "http://localhost:3000/user/image_path/3/thumb/2.jpg", user_name: "TestUser2", created_at: "Fri Mar 28 06:13:49 UTC 2008", email: "testuser2@testurl.com"},{title: "abhijats post", post_id: 58, post_type_id: 1, parent_post_id: 55, description: "", time_zone: "International Date Line West", user_id: 2, image_path: "http://localhost:3000/user/image_path/2/thumb/9.jpg", user_name: "TestUser3", created_at: "Fri Mar 28 11:37:16 UTC 2008", email: "testuser3@testurl.com"},{title: "Give demo", post_id: 41, post_type_id: 4, parent_post_id: null, description: "74strong76Give demo74/strong76", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 06:04:02 UTC 2008", email: "testuser4@testurl.com"},{title: "Status Update", post_id: 37, post_type_id: 5, parent_post_id: null, description: "Working", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 05:20:37 UTC 2008", email: "testuser4@testurl.com"},

{title: "abhijat has read acess", post_id: 57, post_type_id: 1, parent_post_id: 55, description: "", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "AbhilashKumar", created_at: "Fri Mar 28 11:16:50 UTC 2008", email: "testuser4@testurl.com"},

{title: "Inxero", post_id: 48, post_type_id: 2, parent_post_id: null, description: "Inxero", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 06:56:28 UTC 2008", email: "testuser4@testurl.com"},

{title: "New public porjet", post_id: 50, post_type_id: 2, parent_post_id: null, description: "", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 10:30:04 UTC 2008", email: "testuser4@testurl.com"},

{title: "Status Update", post_id: 35, post_type_id: 5, parent_post_id: null, description: "Hey the stuff is coming good", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 01:50:48 UTC 2008", email: "testuser4@testurl.com"},

{title: "Status Update", post_id: 36, post_type_id: 5, parent_post_id: null, description: "kljklkl", time_zone: "Paris", user_id: 1, image_path: "http://localhost:3000/user/image_path/1/thumb/mazewallpaperv2.jpg", user_name: "TestUser4", created_at: "Fri Mar 28 01:59:08 UTC 2008", email: "testuser4@testurl.com"}]}

Here is the JSON parser that will parse the JSON object:

this parser uses $.getJSON() method, with a call to the JSON object file, and a function to pass the data through for parsing.The common $.each() method in jQuery can be used to traverse the “nodes” (keys) that you indicate.

$(function(url) {
 $.getJSON("jsonObj.js", function(json) {

  /* Parse JSON objects */
  jJSON["title"] = (function() {
   response = {
    values: [],
    count: 0
   };
   $.each(json.attributes,function(i,item) {
    if (item.title != "undefined" && item.title != "") {
     response.count++;
     response.values[i] = item.title;
    }
   });
   return response;
  })();

  jJSON["email"] = (function() {
   response = {
    values: [],
    count: 0
   };
   $.each(json.attributes,function(i,item) {
    if (item.email != "undefined") {
     response.count++;
     response.values[i] = item.email;
    }
   });
   return response;
  })();

  jJSON["created_at"] = (function() {
   response = {
    values: [],
    count: 0
   };
   $.each(json.attributes,function(i,item) {
    if (item.created_at != "undefined") {
     response.count++;
     response.values[i] = item.created_at;
    }
   });
   return response;
  })();

  jJSON["image_path"] = (function() {
   response = {
    values: [],
    count: 0
   };
   $.each(json.attributes,function(i,item) {
    if (item.image_path != "undefined") {
     response.count++;
     response.values[i] = item.image_path;
    }
   });
   return response;
  })();

  jJSON["project_name"] = (function() {
   response = {
    values: [],
    count: 0
   };
   $.each(json.attributes,function(i,item) {
    if (item.image_path != "undefined") {
     response.count++;
     response.values[i] = item.project_name;
    }
   });
   return response;
  })();

  /* Function to show data sreams */

  displaySocialStream = function() {
   var displayDiv = $(#post_wrapper);
   var dumpP;
   var str = ;
   for(strmCntr =0; strmCntr < (jJSON["title"]["values"].length - 1); strmCntr++){
    if(strmCntr/2 == 0)
    {
     postClass = "post";
    }else{
     postClass = "post alternate-post";
    }

    str = str + <div class=" + postClass + "><div class="avatar"><img src=" + jJSON["image_path"]["values"][strmCntr]  + " class="avatar-image"/></div><div class="post-content"><b>Created At:</b> + jJSON["created_at"]["values"][strmCntr] + <br /> <b>Project Name:</b> + jJSON["project_name"]["values"][strmCntr]  + <br /> <b>Post Title:</b> + jJSON["title"]["values"][strmCntr]  + <br /> <b>Email:</b> <a href="mailto: + jJSON["email"]["values"][strmCntr]  + "> + jJSON["email"]["values"][strmCntr]  + </a></div></div>;
   }
   displayDiv.prepend(str);
  };
  displaySocialStream();

 });
}
)

The grabbing data from XML is quite different than grabbing data from JSOn, Accessing values will return an array of values, and count will tell you how many of that particular reference exists. For example, how many “title”or “email” key references were obtained from the JSON.

var jJSON = {
    getValues: function(obj,num) {
        return jJSON[obj]["values"].slice(0,((num == null) ? jJSON[obj]["values"].length : num));
    },
    getCount: function(obj) {
        return jJSON[obj]["count"];
    },
    getRandomValues: function(obj,num) {
        var a = [];
        var b = jJSON[obj]["values"];
        var c = b.length;
        if (num != null && num < c) {
            c = num;
        }
        for (i = 0; i < c; i++) {
            var e = Math.floor(Math.random() * b.length);
            a[i] = b[e];
            b.splice(e,1);
        }
        return a;
    }
};

 Original Source:

AddThis Social Bookmark Button

Posted at 12:48:13 pm | Permalink | Posted in JSON  jQuery  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (90)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (80)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (172)
  • JSON (71)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (225)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange