【问题标题】:Access response in JsonJson 中的访问响应
【发布时间】:2021-08-26 23:11:44
【问题描述】:

我正在使用 JSON VBA library 解析大型 JSON 响应,但无法按预期访问响应的元素。

例如响应具有以下结构:

{employees: [{employeeId: {id: 1234}, personNumber: "ABC123", shortName: "Bob",...},...],...}

我试过了:

Dim JsonPayload As Object
Set JsonPayload = JsonConverter.ParseJson(req.ResponseText)


MsgBox JsonPayload("dict_pDictionary")
MsgBox JsonPayload("employees")(1)("employeeId")

还有:

Set emp = JsonPayload("employees")
For Each e In emp
    Debug.Print "employee", e.shortName
    Debug.Print "shortName", e
    Debug.Print "shortName", e.dict_pDictionary
Next e

错误是Object Does not support this property or method.

我想知道如何循环响应。有些是变体/对象/字典,而其他部分似乎是变体/对象/集合

【问题讨论】:

    标签: arrays json excel vba


    【解决方案1】:

    VBA JSON 将对象 ({}) 转换为字典,将数组 ([]) 转换为集合。

    在您的代码中,e 是 Dictionary 对象,而不是具有命名属性的对象,因此:

    Dim jso As Object, emps, e
    
    Set jso = JsonConverter.ParseJson( _
      "{""employees"":[{""employeeId"": {""id"": 1234}, ""personNumber"": ""ABC123"", ""shortName"": ""Bob""}]}")
    
    Set emps = jso("employees")
    For Each e In emps
        Debug.Print e("employeeId")("id")  '>> 1234
        Debug.Print e("shortName")         '>> Bob
    Next e
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2019-08-19
      • 2010-11-19
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多