【问题标题】:Parsing Nested Arrays using VBA and JSON使用 VBA 和 JSON 解析嵌套数组
【发布时间】:2016-11-05 22:44:53
【问题描述】:

我有一个我试图在 VBA 中解析的 JSON。我已成功解析出“优惠”数组。在“offers”数组中是另一个数组“prices”“USD”。

问题在于并非每个“优惠”对象都有“美元”数组。我正在尝试创建一个可以用来制作表格/工作表的对象,但我什至无法在调试模式下打印这些对象。这可行但失败了,因为并非每个 Dict OfferDetails 都包含“USD”对象。

我想做的是能够打印字符串,如果“USD”对象丢失,只需跳过它,只打印具有“USD”的对象。我已经尝试过 IsMissing(在代码中),但是当它碰到丢失的“USD”对象时它失败了。

知道如何使用“USD”值获取此字符串吗?请注意,“USD”是一个数组,包含多个对象,但我也不知道如何处理它们。理想情况下,我想以与“报价”相同的方式解析“美元”。我完全迷失了,因为我在 VBA 方面不是很好

这是一个带有有效 Web JSON 的工作脚本。

  Sub getJSONEP_lib_working()
      'Need the JsonConverter found here https://github.com/VBA-tools/VBA-JSON
      'Need the Microsoft Scripting Runtime 

      Dim Parsed As Dictionary
      Dim Item As Dictionary
      Dim OfferDetails As Dictionary
      Dim Price As Dictionary
      Dim USD As Dictionary

              URL = "http://wraymac.com/JSON/example1.json"
              url2 = "[{" & """mpn""" & ":" & """41202""" & "}]"

              Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
              MyRequest.Open "GET", URL
              MyRequest.Send

              JsonString = MyRequest.ResponseText

              Dim json As Object
              Set json = JsonConverter.ParseJson(JsonString)


      Set Parsed = JsonConverter.ParseJson(MyRequest.ResponseText)

         For Each Item In Parsed("results")(1)("items")
           For Each OfferDetails In Item("offers")


      'I tried this it doesn't work, it fails when it finds a non existent "USD"
            If Not IsMissing(OfferDetails("prices")("USD")(1)(1)) Then
            Debug.Print OfferDetails("prices")("USD")(1)(1)
            Else
            Debug.Print "Missing"
            End If


      x = Item("mpn") & "   " & "sku" & " - " & OfferDetails("sku") & "," & "UID" & " - " & OfferDetails("seller")("uid") & "   " & OfferDetails("moq") & "packaging" & " = " & OfferDetails("packaging") & "  " & OfferDetails("seller")("name") & "  " & Item("manufacturer")("name")
      Debug.Print x

      'This works but fails because not every Dict OfferDetails contains the "USD" object
      'x = Item("mpn") & "   " & "sku" & " - " & OfferDetails("sku") & "," & "UID" & " - " & OfferDetails("seller")("uid") & "   " & OfferDetails("moq") & "packaging" & " = " & OfferDetails("packaging") & "  " & OfferDetails("seller")("name") & "  " & Item("manufacturer")("name")& "  "&OfferDetails("prices")("USD")(1)(1)

       Next OfferDetails
           Next

      End Sub

【问题讨论】:

标签: arrays json excel vba ms-access


【解决方案1】:

你想使用 Dictionary 的Exists 方法:

Set Parsed = JsonConverter.ParseJson(MyRequest.ResponseText)

For Each Item In Parsed("results")(1)("items")
    For Each OfferDetails In Item("offers")

            If OfferDetails("prices").Exists("USD") Then
                Debug.Print OfferDetails("prices")("USD").Count & " items:"
                Debug.Print "-----------------"
                For Each x In OfferDetails("prices")("USD")
                    Debug.Print x(1), x(2)
                Next x
                Debug.Print "-----------------"
            Else
                Debug.Print "No USD"
            End If

     Next OfferDetails
 Next

转换器将对象 ({}) 解析为字典,将数组 ([]) 解析为集合,因此您可以使用 Count 来确定每种对象类型中的项目数。

【讨论】:

  • 工作,谢谢。现在,如果我能弄清楚如何处理所有美元条目,它们都有不同的值。我想我可以在代码中引用每一个,但它们会改变。例如 offer (0) 或 (1)vba 有四个条目, offer (1) (2)vba 有八个。但这很好用,再次感谢您
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
相关资源
最近更新 更多