2 Replies - 4711 Views - Last Post: 26 November 2011 - 03:09 PM

#1 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

parsing json with sub-catagories (using jquery)

Posted 25 November 2011 - 07:02 PM

I'm currently able to parse a JSON file with this structure:

{
	"pkg1" : "Package name",
	"pkg2" : "Price $0.00",
	"pkg3" : "Long description sentence."
}



Using this jQuery:

$.getJSON("package.json", function (data) {
                $.each(data, function (key, value) {
                    $("<p/>").attr("id", key).html(value).appendTo("#content");
                });
            });



How can I structure a JSON file to have 2 dimensions?
And how can I tell jQuery to find a certain catagory?
I was thinking of using

if (key == "package1"){



Is This A Good Question/Topic? 0
  • +

Replies To: parsing json with sub-catagories (using jquery)

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: parsing json with sub-catagories (using jquery)

Posted 26 November 2011 - 12:55 PM

I am not exactly sure what your query is. Do you want to know how to parse JSON files with deeper nesting? I am giving an example here, some code I wrote long ago, see if it helps:

JSON (dic.json):

[
 {
   "term": "aardvark",
   "part": "Noun.",
   "meaning": "an animal from southern Africa that has a long nose and tongue and that eats insects",
   "quotes": [
                    {
					  "quote": "Aardvark is funny!",
					   "author": "Kaustav Mukherjee"
					   },
					 {
                       "quote": "Aardvarks are hot!",
                       "author": "Mayukh Mukherjee"
                     },
                     {
                        "quote": "Aardvarks are sexy!",
                        "author": "Sayak Das"
                      }
                 ]
	},
...
]



jQuery Snippet:

$.getJSON('dic.json', function(data) {
$.each(data, function(index, entry) {
var html = "<div id = 'word'>";
html+= "<p class = 'term'>" + "<span>" + entry['term']+ "</span>";
html+= "<sub>" + entry['part']+ "</sub>" + "</p>";
html+= "<p class = 'meaning'>" + entry['meaning'] + "</p>";
html+= "<ul>";
$.each(entry['quotes'], function(ind,line) {
html+="<li class = 'line'>"+"<div class = 'quote'>"+funci(line['quote'], entry['term'])+"</div>"+"</li>";
html+="<li class = 'author'>"+"-"+line['author']+"</li>";
html+="<li class = 'clear'>"+"</li>";
});
html+="</ul>";
html+= "</div>";
buffer = reserve;
$(".dic").append(html);
});
});


This post has been edited by cupidvogel: 26 November 2011 - 12:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: parsing json with sub-catagories (using jquery)

Posted 26 November 2011 - 03:09 PM

Yes that's exactly what I was trying to do.

Thank you very much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1