从JSON访问元素

时间:2015-05-16 20:17:50

标签: json vba

我正在尝试访问" id"," first_name"来自Excel vba中的json。

{
    "paging": {
        "per_page": 10,
        "page": 1,
        "previous": null,
        "self": "/api/v1/users?per_page=10&page=1",
        "next": null
    },
    "data": [
        {
            "id": 15093,
            "first_name": "ala",
            "last_name": "adasd",
            "display_name": "Ali Saigar",
            "email": "aadaasdr@yahoo.co.uk",
            "user_type_id": 1,
            "billable": true,
            "hire_date": null,
            "termination_date": null,
            "mobile_phone": null,
            "office_phone": null,
            "deleted": false,
            "deleted_at": null,
            "account_owner": true,
            "invitation_pending": false,
            "user_settings": 786568,
            "guid": "c010323e-bf44-4ed1-ac70-85858475df14",
            "employee_number": null,
            "billability_target": 100,
            "billrate": -1,
            "role": null,
            "discipline": null,
            "location": null,
            "has_login": true,
            "thumbnail": ""
        },
        {
            "id": 15094,
            "first_name": "James",
            "last_name": "Bond",
            "display_name": "James Bond",
            "email": null,
            "user_type_id": 3,
            "billable": true,
            "hire_date": null,
            "termination_date": null,
            "mobile_phone": null,
            "office_phone": null,
            "deleted": false,
            "deleted_at": null,
            "account_owner": false,
            "invitation_pending": false,
            "user_settings": 0,
            "guid": "91e6a979-1753-46a0-bae3-37cf4003e953",
            "employee_number": null,
            "billability_target": 100,
            "billrate": -1,
            "role": null,
            "discipline": null,
            "location": null,
            "has_login": false,
            "thumbnail": ""
        }
    ]
}

任何人都可以告诉我如何从vba访问它吗? 我尝试了很多方法,但无法成功,

1 个答案:

答案 0 :(得分:1)

可替换地:

Sub JsonTest()

    Dim json As String
    Dim sc As Object
    Dim o

    Set sc = CreateObject("scriptcontrol")
    sc.Language = "JScript"

    json = Range("A1").Value 'for my testing...

    sc.eval "var obj=(" & json & ")" 'evaluate the json

    Debug.Print sc.eval("obj.data.length")
    Debug.Print sc.eval("obj.data[0].id")
    Debug.Print sc.eval("obj.data[0].email")
    Debug.Print sc.eval("obj.data[0].guid")

End Sub