如何使用jquery解析多维json数组

时间:2012-04-26 11:53:19

标签: jquery html json

我有以下jquery函数来解析json数组“projectDetail”,但是当我尝试从显示为undefined的数组中打印ApprovalDesc的值时,我能否知道什么是正确的语法?

function show(projectDetail) {
          var  project = projectDetail;
            $.each(project, function (i, tweet) {
                $.each(tweet.ProjectUpdates, function (i, tweet1) {
                $.each(tweet1.ProjectApproval, function (i, tweet2) {
                alert(i + ': ' + tweet2.ApprovalDesc);
               var PStatus = tweet.ApprovalDesc;
            });
        });
        });
       }

json输出

{ "d" : {
      "ProjectUpdates" : { "ExceptionId" : 0,
          "ProjectApproval" : [ { "ApprovalDesc" : "ABCApproved",
                "CreatedBy" : null,
                "CreatedDate" : "/Date(-62135596800000)/", 
                "ModifiedDate" : "/Date(-62135596800000)/",
                "ApprovalDesc" : "welcome",
                "SortOrder" : 1
              } ],
          "ProjectStatusGallery" : [ { "CategoryId" : 0,
                "CreatedBy" : null,
                "CreatedDate" : "/Date(-62135596800000)/",              
                "ImageName" : "flower",
                "ImageUrl" : "D://Images",
                "ModifiedBy" : null,
                "ModifiedDate" : "/Date(-62135596800000)/",               
              },
              { "CategoryId" : 0,
                "CreatedBy" : null,
                "CreatedDate" : "/Date(-62135596800000)/",                
                "ImageName" : "flower2",
                "ImageUrl" : "D://Images",
                "ModifiedBy" : null,
                "ModifiedDate" : "/Date(-62135596800000)/"                
              }
            ]
        },
 "__type" : "sample"
    } }

2 个答案:

答案 0 :(得分:1)

welcome 应引用为字符串。

答案 1 :(得分:0)

你有没有在json字符串上调用parseJSON把它放在一个对象中?然后它易于访问,无需循环。

http://api.jquery.com/jQuery.parseJSON/

var json = $.parseJSON(your_json_string);

console.log(json.__type.sample);
console.log(json.d.ProjectUpdates.ProjectStatusGallery.ImageName);