John John
John John

Reputation: 1

how can i access Json object that have hierarchy of data inside it

i usally work with Json objects on my asp.net mvc by looping through the object and display the result such as

$.each(data, function (key, val) { 

                        // Format the text to display. 
                        var str = val.id + ': $' + val.packageName; 

                        // Add a list item for the product. 
                        $('<li/>', { text: str }) 
                        .appendTo($('#products')); 

But i came across a web service that return Json object in the following format that contains some sort of hireachy:-

{
  "total":3,
  "desc":false,
  "sort":"name",
  "start":0,
  "data":
  [
    {"id":"hdc_v1001#1#hdc_certification_process",
      "packageName":"halal certification",
      "name":"HDC Certification Process",
      "packageId":"hdc_v1001",
      "label":"HDC Certification Process ver 1",
      "version":"1"},
    {"id":"mdec_v1002#12#mdec_wp1",
      "packageName":"Mdec Work Progress",
      "name":"mdec_wp1",
      "packageId":"mdec_v1002",
      "label":"mdec_wp1 ver 12",
      "version":"12"},
    {"id":"mora#5#mora_new_application",
      "packageName":"MORA Halal Certification",
      "name":"MORA New Application",
      "packageId":"mora",
      "label":"MORA New Application ver 5",
      "version":"5"}
  ]
}

So how i will be able to loop through the Json object in this case to displayed its values ? Best Regards

Upvotes: 0

Views: 245

Answers (1)

Salketer
Salketer

Reputation: 15711

myJsonObject = {
  "total":3,
  "desc":false,
  "sort":"name",
  "start":0,
  "data":
  [
    {"id":"hdc_v1001#1#hdc_certification_process",
      "packageName":"halal certification",
      "name":"HDC Certification Process",
      "packageId":"hdc_v1001",
      "label":"HDC Certification Process ver 1",
      "version":"1"},
    {"id":"mdec_v1002#12#mdec_wp1",
      "packageName":"Mdec Work Progress",
      "name":"mdec_wp1",
      "packageId":"mdec_v1002",
      "label":"mdec_wp1 ver 12",
      "version":"12"},
    {"id":"mora#5#mora_new_application",
      "packageName":"MORA Halal Certification",
      "name":"MORA New Application",
      "packageId":"mora",
      "label":"MORA New Application ver 5",
      "version":"5"}
  ]
};
$.each(myJsonObject.data, function (key, val) { 

                    // Format the text to display. 
                    var str = val.id + ': $' + val.packageName; 

                    // Add a list item for the product. 
                    $('<li/>', { text: str }) 
                    .appendTo($('#products')); 
}

Upvotes: 1

Related Questions