【问题标题】:how to extract elements in json string in vba如何在vba中提取json字符串中的元素
【发布时间】:2018-12-15 16:03:44
【问题描述】:

我有以下 json 字符串:

[
    {
        "Features": {
            "Level": [
                {
                    "endDate": "2018-12-11",
                    "minimum": "0.000000000000",
                    "maximum": "0.000000000000",
                    "value": "228.108000000000",
                    "payDate": "0"
                },
                {
                    "endDate": "2018-12-11",
                    "minimum": "0.000000000000",
                    "maximum": "0.000000000000",
                    "value": "3143.513000000000",
                    "payDate": "0"
                }
            ]
        }
    },
]

我试图使用 vba 在 excel 中提取这两个值并具有以下代码(其中 result2 将是 json 字符串)

Public Sub GetS (result2 As String, m As Integer)

Dim activeWS As Worksheet
Set activeWS = ThisWorkbook.Worksheets("Data")
Dim jsonStr As String, json As Object, headers()
jsonStr = result2 
Set json = JsonConverter.ParseJson(jsonStr)(1)
activeWS.Cells(m, 19) = json("Features")("Level")(0)("value")
activeWS.Cells(m, 20) = json("Features")("Level")(1)("value")


End Sub

vba 的第二部分工作,它获取 3143.51(第二个数字)的值,我想知道如何获得第一个值 (228.10)。

我尝试过使用 ("Initial Level")(0)("value") 但它不起作用。

非常感谢。

【问题讨论】:

    标签: json vba


    【解决方案1】:

    JSON 数组被转换为 VBA 集合 - 这些是从 1 开始索引而不是从零开始

    试试这个:

    jsonStr = Range("I10").Value 'your json sample
    Set json = JsonConverter.ParseJson(jsonStr)(1)
    
    Debug.Print json("Features")("Level")(1)("value") '>>228.108000000000
    Debug.Print json("Features")("Level")(2)("value") '>>3143.513000000000
    

    (测试并确认)

    【讨论】:

    • 嗨,蒂姆,它仍然无法正常工作。另外,请注意 json("Features")("Level")(1)("value") 会获取第二个值(不是第一个值),那么我有什么方法可以获取第一个值吗?谢谢你!
    • 我可以测试,但您发布的 JSON 无法解析,我不想猜测修复方法。
    • 我对其进行了编辑,现在应该可以使用了,非常感谢!
    • 在测试中确认使用您的(几乎可以正常工作 - 最后一个大括号后的额外逗号)json
    • 非常感谢!
    【解决方案2】:

    以下是如何访问 JSON 字符串中的数据:

    Json("match")("awayTeam")("coach")("countryOfBirth")
    Json("match")("awayTeam")("coach")("nationality")
    Json("match")("awayTeam")("captain")("id")
    Json("match")("awayTeam")("captain")("name")
    Json("match")("awayTeam")("captain")("shirtNumber")
    Json("match")("awayTeam")("lineup")(1)("id")
    Json("match")("awayTeam")("lineup")(1)("name")
    Json("match")("awayTeam")("lineup")(1)("position")
    Json("match")("awayTeam")("lineup")(1)("shirtNumber")
    Json("match")("awayTeam")("lineup")(2)("id")
    Json("match")("awayTeam")("lineup")(2)("name")
    Json("match")("awayTeam")("lineup")(2)("position")
    Json("match")("awayTeam")("lineup")(2)("shirtNumber")
    Json("match")("awayTeam")("lineup")(3)("id")
    Json("match")("awayTeam")("lineup")(3)("name")
    Json("match")("awayTeam")("lineup")(3)("position")
    Json("match")("awayTeam")("lineup")(3)("shirtNumber")
    Json("match")("awayTeam")("lineup")(4)("id")
    Json("match")("awayTeam")("lineup")(4)("name")
    Json("match")("awayTeam")("lineup")(4)("position")
    Json("match")("awayTeam")("lineup")(4)("shirtNumber")
    Json("match")("awayTeam")("lineup")(5)("id")
    Json("match")("awayTeam")("lineup")(5)("name")
    Json("match")("awayTeam")("lineup")(5)("position")
    Json("match")("awayTeam")("lineup")(5)("shirtNumber")
    Json("match")("awayTeam")("lineup")(6)("id")
    Json("match")("awayTeam")("lineup")(6)("name")
    Json("match")("awayTeam")("lineup")(6)("position")
    Json("match")("awayTeam")("lineup")(6)("shirtNumber")
    Json("match")("awayTeam")("lineup")(7)("id")
    Json("match")("awayTeam")("lineup")(7)("name")
    Json("match")("awayTeam")("lineup")(7)("position")
    Json("match")("awayTeam")("lineup")(7)("shirtNumber")
    Json("match")("awayTeam")("lineup")(8)("id")
    Json("match")("awayTeam")("lineup")(8)("name")
    Json("match")("awayTeam")("lineup")(8)("position")
    Json("match")("awayTeam")("lineup")(8)("shirtNumber")
    Json("match")("awayTeam")("lineup")(9)("id")
    Json("match")("awayTeam")("lineup")(9)("name")
    Json("match")("awayTeam")("lineup")(9)("position")
    Json("match")("awayTeam")("lineup")(9)("shirtNumber")
    Json("match")("awayTeam")("lineup")(10)("id")
    Json("match")("awayTeam")("lineup")(10)("name")
    Json("match")("awayTeam")("lineup")(10)("position")
    Json("match")("awayTeam")("lineup")(10)("shirtNumber")
    Json("match")("awayTeam")("lineup")(11)("id")
    Json("match")("awayTeam")("lineup")(11)("name")
    Json("match")("awayTeam")("lineup")(11)("position")
    Json("match")("awayTeam")("lineup")(11)("shirtNumber")
    Json("match")("awayTeam")("bench")(1)("id")
    Json("match")("awayTeam")("bench")(1)("name")
    Json("match")("awayTeam")("bench")(1)("position")
    Json("match")("awayTeam")("bench")(1)("shirtNumber")
    Json("match")("awayTeam")("bench")(2)("id")
    Json("match")("awayTeam")("bench")(2)("name")
    Json("match")("awayTeam")("bench")(2)("position")
    Json("match")("awayTeam")("bench")(2)("shirtNumber")
    Json("match")("awayTeam")("bench")(3)("id")
    Json("match")("awayTeam")("bench")(3)("name")
    Json("match")("awayTeam")("bench")(3)("position")
    Json("match")("awayTeam")("bench")(3)("shirtNumber")
    Json("match")("awayTeam")("bench")(4)("id")
    Json("match")("awayTeam")("bench")(4)("name")
    Json("match")("awayTeam")("bench")(4)("position")
    Json("match")("awayTeam")("bench")(4)("shirtNumber")
    Json("match")("awayTeam")("bench")(5)("id")
    Json("match")("awayTeam")("bench")(5)("name")
    Json("match")("awayTeam")("bench")(5)("position")
    Json("match")("awayTeam")("bench")(5)("shirtNumber")
    Json("match")("awayTeam")("bench")(6)("id")
    Json("match")("awayTeam")("bench")(6)("name")
    Json("match")("awayTeam")("bench")(6)("position")
    Json("match")("awayTeam")("bench")(6)("shirtNumber")
    Json("match")("awayTeam")("bench")(7)("id")
    Json("match")("awayTeam")("bench")(7)("name")
    Json("match")("awayTeam")("bench")(7)("position")
    Json("match")("awayTeam")("bench")(7)("shirtNumber")
    Json("match")("goals")(1)("minute")
    Json("match")("goals")(1)("extraTime")
    Json("match")("goals")(1)("type")
    Json("match")("goals")(1)("team")("id")
    Json("match")("goals")(1)("team")("name")
    Json("match")("goals")(1)("scorer")("id")
    Json("match")("goals")(1)("scorer")("name")
    Json("match")("goals")(1)("assist")("id")
    Json("match")("goals")(1)("assist")("name")
    Json("match")("goals")(2)("minute")
    Json("match")("goals")(2)("extraTime")
    Json("match")("goals")(2)("type")
    Json("match")("goals")(2)("team")("id")
    Json("match")("goals")(2)("team")("name")
    Json("match")("goals")(2)("scorer")("id")
    Json("match")("goals")(2)("scorer")("name")
    Json("match")("goals")(2)("assist")("id")
    Json("match")("goals")(2)("assist")("name")
    Json("match")("goals")(3)("minute")
    Json("match")("goals")(3)("extraTime")
    Json("match")("goals")(3)("type")
    Json("match")("goals")(3)("team")("id")
    Json("match")("goals")(3)("team")("name")
    Json("match")("goals")(3)("scorer")("id")
    Json("match")("goals")(3)("scorer")("name")
    Json("match")("goals")(3)("assist")
    Json("match")("bookings")(1)("minute")
    Json("match")("bookings")(1)("team")("id")
    Json("match")("bookings")(1)("team")("name")
    Json("match")("bookings")(1)("player")("id")
    Json("match")("bookings")(1)("player")("name")
    Json("match")("bookings")(1)("card")
    Json("match")("bookings")(2)("minute")
    Json("match")("bookings")(2)("team")("id")
    Json("match")("bookings")(2)("team")("name")
    Json("match")("bookings")(2)("player")("id")
    Json("match")("bookings")(2)("player")("name")
    Json("match")("bookings")(2)("card")
    Json("match")("bookings")(3)("minute")
    Json("match")("bookings")(3)("team")("id")
    Json("match")("bookings")(3)("team")("name")
    Json("match")("bookings")(3)("player")("id")
    Json("match")("bookings")(3)("player")("name")
    Json("match")("bookings")(3)("card")
    Json("match")("bookings")(4)("minute")
    Json("match")("bookings")(4)("team")("id")
    Json("match")("bookings")(4)("team")("name")
    Json("match")("bookings")(4)("player")("id")
    Json("match")("bookings")(4)("player")("name")
    Json("match")("bookings")(4)("card")
    Json("match")("bookings")(5)("minute")
    Json("match")("bookings")(5)("team")("id")
    Json("match")("bookings")(5)("team")("name")
    Json("match")("bookings")(5)("player")("id")
    Json("match")("bookings")(5)("player")("name")
    Json("match")("bookings")(5)("card")
    Json("match")("bookings")(6)("minute")
    Json("match")("bookings")(6)("team")("id")
    Json("match")("bookings")(6)("team")("name")
    Json("match")("bookings")(6)("player")("id")
    Json("match")("bookings")(6)("player")("name")
    Json("match")("bookings")(6)("card")
    Json("match")("bookings")(7)("minute")
    Json("match")("bookings")(7)("team")("id")
    Json("match")("bookings")(7)("team")("name")
    Json("match")("bookings")(7)("player")("id")
    Json("match")("bookings")(7)("player")("name")
    Json("match")("bookings")(7)("card")
    Json("match")("substitutions")(1)("minute")
    Json("match")("substitutions")(1)("team")("id")
    Json("match")("substitutions")(1)("team")("name")
    Json("match")("substitutions")(1)("playerOut")("id")
    Json("match")("substitutions")(1)("playerOut")("name")
    Json("match")("substitutions")(1)("playerIn")("id")
    Json("match")("substitutions")(1)("playerIn")("name")
    Json("match")("substitutions")(2)("minute")
    Json("match")("substitutions")(2)("team")("id")
    Json("match")("substitutions")(2)("team")("name")
    Json("match")("substitutions")(2)("playerOut")("id")
    Json("match")("substitutions")(2)("playerOut")("name")
    Json("match")("substitutions")(2)("playerIn")("id")
    Json("match")("substitutions")(2)("playerIn")("name")
    Json("match")("substitutions")(3)("minute")
    Json("match")("substitutions")(3)("team")("id")
    Json("match")("substitutions")(3)("team")("name")
    Json("match")("substitutions")(3)("playerOut")("id")
    Json("match")("substitutions")(3)("playerOut")("name")
    Json("match")("substitutions")(3)("playerIn")("id")
    Json("match")("substitutions")(3)("playerIn")("name")
    Json("match")("substitutions")(4)("minute")
    Json("match")("substitutions")(4)("team")("id")
    Json("match")("substitutions")(4)("team")("name")
    Json("match")("substitutions")(4)("playerOut")("id")
    Json("match")("substitutions")(4)("playerOut")("name")
    Json("match")("substitutions")(4)("playerIn")("id")
    Json("match")("substitutions")(4)("playerIn")("name")
    Json("match")("substitutions")(5)("minute")
    Json("match")("substitutions")(5)("team")("id")
    Json("match")("substitutions")(5)("team")("name")
    Json("match")("substitutions")(5)("playerOut")("id")
    Json("match")("substitutions")(5)("playerOut")("name")
    Json("match")("substitutions")(5)("playerIn")("id")
    Json("match")("substitutions")(5)("playerIn")("name")
    Json("match")("substitutions")(6)("minute")
    Json("match")("substitutions")(6)("team")("id")
    Json("match")("substitutions")(6)("team")("name")
    Json("match")("substitutions")(6)("playerOut")("id")
    Json("match")("substitutions")(6)("playerOut")("name")
    Json("match")("substitutions")(6)("playerIn")("id")
    Json("match")("substitutions")(6)("playerIn")("name")
    Json("match")("referees")(1)("id")
    Json("match")("referees")(1)("name")
    Json("match")("referees")(1)("nationality")
    Json("match")("referees")(2)("id")
    Json("match")("referees")(2)("name")
    Json("match")("referees")(2)("nationality")
    Json("match")("referees")(3)("id")
    Json("match")("referees")(3)("name")
    Json("match")("referees")(3)("nationality")
    Json("match")("referees")(4)("id")
    Json("match")("referees")(4)("name")
    Json("match")("referees")(4)("nationality")
    

    下面的两个答案展示了如何使用我编写的函数PrintJSONAccessors() 来解开JsonConverter.ParseJson() 生成的集合和数组。

    【讨论】:

      猜你喜欢
      • 2011-05-27
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多