0

我通常在我的 asp.net mvc 上使用 Json 对象,方法是遍历对象并显示结果,例如

$.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')); 

但是我遇到了一个 Web 服务,它以以下格式返回 Json 对象,其中包含某种类型的内容:-

{
  "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"}
  ]
}

那么在这种情况下,我如何能够遍历 Json 对象以显示其值?最好的祝福

4

1 回答 1

1
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')); 
}
于 2012-10-09T10:18:34.140 回答