Parse Json嵌入页面

时间:2017-05-18 19:29:00

标签: jquery json

我在页面中有以下对象:

"scripts": {
  "postcheckout": "npm install", // <-- Git hook from Husky
  "postinstall": "gulp build"    // <-- default npm hook
}

如何使用jQuery显示每个案例?我可以获取caseMgtData.cases [0] .status并显示,但我该怎么做每个或foreach并显示数据?

1 个答案:

答案 0 :(得分:2)

这只是一个全局的javascript对象,而不是JSON,所以你可以通过它来操纵它,通常是

for (var i = 0; i < caseMgtData.cases.length; i++) {
  console.log(caseMgtData.cases[i].subject);
}
<script type="text/javascript">var caseMgtData =
{
"totalNumOfCase": 2,
"cases": [{
    "id": "50017000003fWF4AAM",
    "caseNumber": "00007764",
    "status": "New",
    "priority": "Low",
    "subject": "Issue summary goes here",
    "description": "Full Description Goes here",
    "caseOwner": "00Go00000018IpCEAU",
    "contact": {
        "recordId": "00317000005XJUCAA4",
        "name": "blah Herring",
        "firstName": "blah",
        "lastName": "blah",
        "email": "blah@blah.com.x",
        "phone": "5209012560",
        "mobile": "5204655312"
    },
    "timeOpened": "2016-02-22T05:12:16.000+0000",
    "caseOrigin": "Web"
}, {
    "id": "50017000001NVoyAAG",
    "caseNumber": "00002009",
    "status": "Closed",
    "priority": "Low",
    "subject": "blah last test",
    "description": "blah last test",
    "caseOwner": "00517000000YfBJAA0",
    "contact": {
        "recordId": "00317000005XJUCAA4",
        "name": "Blah blah",
        "firstName": "blah",
        "lastName": "Herring",
        "email": "blah@blah.com.x",
        "phone": "5209012560",
        "mobile": "5204655312"
    },
    "timeOpened": "2015-03-10T17:34:36.000+0000",
    "timeClosed": "2015-06-03T05:37:16.000+0000",
    "caseOrigin": "Web"
}]
}
</script>

<body></body>