从API JSON创建查询

时间:2018-11-25 19:14:12

标签: javascript php json

我正在使用Brightspace API。我可以获取JSON数据,但无法搜索字段。 这是JSON输出:

{
    "PagingInfo": {
        "Bookmark": "392854",
        "HasMoreItems": false
    },
    "Items": [
        {
            "User": {
                "Identifier": "58686",
                "DisplayName": "SDent",
                "EmailAddress": "",
                "OrgDefinedId": "009700001",
                "ProfileBadgeUrl": null,
                "ProfileIdentifier": "Kx9BKKwtp3"
            },
            "Role": {
                "Id": 103,
                "Code": "01",
                "Name": "Student"
            }
        },
        {
            "User": {
                "Identifier": "58687",
                "DisplayName": "S2Dent",
                "OrgDefinedId": "009700002",
                "ProfileBadgeUrl": null,
                "ProfileIdentifier": "sybvFAVR3N"
            },
            "Role": {
                "Id": 103,
                "Code": "01",
                "Name": "Student"
            }
        },
        {
            "User": {
                "Identifier": "361398",
                "DisplayName": "JD",
                "OrgDefinedId": "0035678",
                "ProfileBadgeUrl": null,
                "ProfileIdentifier": "FIBX5sZOtu"
            },
            "Role": {
                "Id": 103,
                "Code": "01",
                "Name": "Student"
            }
        }
    ]
}

如何获取等于009700001的OrgDefinedId字段?我没有使用数据库。 这是我如何获取Json数据。感谢您的帮助。

function getStudentID(orgUnitId){
  hideData();
  document.getElementById("GETField").checked = true;
  document.getElementById("actionField").value = "/d2l/api/lp/1.0/enrollments/orgUnits/"+orgUnitId+"/users/";
  } 

1 个答案:

答案 0 :(得分:0)

您可以使用find方法。

let jsonObj = {
    "PagingInfo": {
        "Bookmark": "392854",
        "HasMoreItems": false
    },
    "Items": [
        {
            "User": {
                "Identifier": "58686",
                "DisplayName": "SDent",
                "EmailAddress": "",
                "OrgDefinedId": "009700001",
                "ProfileBadgeUrl": null,
                "ProfileIdentifier": "Kx9BKKwtp3"
            },
            "Role": {
                "Id": 103,
                "Code": "01",
                "Name": "Student"
            }
        },
        {
            "User": {
                "Identifier": "58687",
                "DisplayName": "S2Dent",
                "OrgDefinedId": "009700002",
                "ProfileBadgeUrl": null,
                "ProfileIdentifier": "sybvFAVR3N"
            },
            "Role": {
                "Id": 103,
                "Code": "01",
                "Name": "Student"
            }
        },
        {
            "User": {
                "Identifier": "361398",
                "DisplayName": "JD",
                "OrgDefinedId": "0035678",
                "ProfileBadgeUrl": null,
                "ProfileIdentifier": "FIBX5sZOtu"
            },
            "Role": {
                "Id": 103,
                "Code": "01",
                "Name": "Student"
            }
        }
    ]
}

let result = jsonObj.Items.find(item=> item.User.OrgDefinedId==="009700001")
console.log(result)