【问题标题】:Loop JSON in array for same value in VBA在数组中循环 JSON 以获得 VBA 中的相同值
【发布时间】:2019-07-04 09:49:09
【问题描述】:

我正在尝试从下面的 json 中的不同列中获取“image_id”。

[
{
"spin": "HM4C6L",
"attributes": {
"product name": "Everest Kutilal Coarse Ground Red Chilli Powder ",
},
"bar_code": {
"valid": true,
"id": "89017817",
"type": "UPC"
},
"spin_state": "LIVE",
"meta": {
"updated-by": "undefined"
},
"version": null,
"images": [
{
"image_id": "dvuewrnauxdroqcapjiu",
"image_name": "masala and spice_HM4A7I2C6L_MN.JPG",
"shot_type": "MN"
},
{
"image_id": "tcku7lwarkv8ch0ao9cu",
"image_name": "masala and spice_HM4A7I2C6L_AL1.JPG",
"shot_type": "AL1"
},
{
"image_id": "b2znlmm59plprrkmkujs",
"image_name": "masala and spice_HM4A7I2C6L_AL2.jpg",
"shot_type": "AL2"
}
]
}
]

我试过Cannot iterate when parsing HTML table using JSON-VBALoop through the JSON object keys in excel vba

Sub getimage()

Dim current As Workbook
Dim sht As Worksheet
Dim a, b, strUrl As String
Dim count As Variant

Set current = ActiveWorkbook

For Each sht In current.Worksheets

    On Error Resume Next
    'Application.ScreenUpdating = False
    Set HTTPReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    count = Range("A1", Range("A1").End(xlDown)).Rows.count

    For i = 2 To count

        a = CStr(Range("A" & i).Value)
        HTTPReq.Open "GET", "link" & a, False
        HTTPReq.send
        'Debug.Print HTTPReq.ResponseText

        Dim Json, item As Object
        Set Json = JsonConverter.ParseJson(HTTPReq.ResponseText)

        For Each item In Json
            Debug.Print item("images")
            sht.Cells(i, B) = item("image_id")("1")
            sht.Cells(i, B) = item("image_id")("2")    
        next Item

    Next i
    'Application.ScreenUpdating = True

End If
Next sht

End Sub

我在单元格 B2 中需要“dvuewrnauxdroqcapjiu”,在单元格 C2 中需要 tcku7lwarkv8ch0ao9cu,在单元格 C2 中需要“b2znlmm59plprrkmkujs”,但我的代码没有输出,也没有错误。

【问题讨论】:

  • 可能在某个地方发生了错误。删除On Error Resume Next,让代码再次运行并检查是否抛出任何错误,发生在哪一行以及它说什么。

标签: json excel vba web-scraping


【解决方案1】:

很多东西。

  1. 您的 json 格式错误。这里有一个额外的“,”:

"Everest Kutilal Coarse Ground Red Chilli Powder ",

这意味着 jsonconverter 会抛出一个错误。最后的“,”将当前键值对与下一个键值对分开。没有后续对,因此应将其删除。

  1. 您的访问路径错误。

这里我正在从单元格 A1 中读取更正后的 json

Option Explicit   
Public Sub test()
    Dim json As Object, i As Long, item As Object, c As Long
    i = 2: c = 2
    Set json = JsonConverter.ParseJson([A1])(1)("images")
    For Each item In json
        ActiveSheet.Cells(i, c) = item("image_id")
        c = c + 1
    Next
End Sub
  1. Cells(2,B) 将期望 B 是一个变量,因为字符串文字包含在 "" 即 "B" 中。此外,您需要一个递增的计数器变量,否则您将继续写入同一个单元格。

【讨论】:

  • 太棒了。是否有如何读取 JSON 文件并检测 JSON 是否正确的链接?
  • 大多数 json 查看器会在验证方面为您执行此操作,但您也可以使用特定的验证器,例如codebeautify.org/jsonvalidator
  • 感谢您的回答和验证 json 的链接。我通过codebeautify.org/jsonvalidator 检查了我的 json,它显示“有效 JSON”。但是,您提供的代码确实帮助了我.. 正确的代码: Set json = JsonConverter.ParseJson(A1)(1)(["images"]) 非常感谢 :)
  • 嗨,那么您的 json 与您在问题中发布的不一样。如果粘贴上面的 json 会出错。但很高兴我们到了那里:-)
  • 是的,我刚刚检查过..对不起...我无法分享原件,所以我手动进行了一些更改,然后我会犯一些错误..但无论如何,你确实解决了我的问题。 。 再次感谢。 :)
猜你喜欢
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多