访问VBA解析json

Posted

技术标签:

【中文标题】访问VBA解析json【英文标题】:Access VBA to parse json 【发布时间】:2016-02-28 18:27:09 【问题描述】:

有人可以帮助使用 Access VBA 代码来解析以下 json 文本吗?我需要检索作者、标题、评论、original_title、original_review、stars、iso、版本、日期、产品、重量和 id 的值,然后循环到下一个条目和下一个条目。


"total": 140,
"pages": 28,
"this_page": 1,
"reviews": [
    "author": "DeveloperToDeveloper",
    "title": "Just Spectacular",
    "review": "Finally able to remove the ads! The description is hilarious!! Thanks!!!",
    "original_title": null,
    "original_review": null,
    "stars": "5.00",
    "iso": "US",
    "version": "1.2",
    "date": "2012-09-19T17:05:00",
    "product": 6567,
    "weight": 0,
    "id": "5561747L7xnbsMRu8UbPvy7A71Dv6A=="
, 
    "author": null,
    "title": "Boat finder",
    "review": "Very good app for my use, regrefully does not Search vessel by name.",
    "original_title": null,
    "original_review": null,
    "stars": "5.00",
    "iso": "US",
    "version": "1.32",
    "date": "2012-08-10T21:15:56",
    "product": 5561747,
    "weight": 0,
    "id": "008bVy7L7xnbsMRu8UbPvy7xx1Dv6A=="
, 
    "author": "Dito",
    "title": "Boat finder",
    "review": "Very good app for my use, regrefully does not Search vessel by name.",
    "original_title": null,
    "original_review": null,
    "stars": "5.00",
    "iso": "US",
    "version": "1.32",
    "date": "2012-08-10T21:15:56",
    "product": 5561,
    "weight": 0,
    "id": "5561747L7xhfgdbs09oyvy7A71Dv6A"
, 
    "author": "KTM SX",
    "title": "À quand le deuxième épisode",
    "review": "Super une maj pour d'autre decore et autre",
    "original_title": null,
    "original_review": null,
    "stars": "5.00",
    "iso": "FR",
    "version": "1.2",
    "date": "2012-07-19T13:58:00",
    "product": 5858,
    "weight": 0,
    "id": "5561747L7xnbsMRu8UbP9s9s754gdt"
 ]

【问题讨论】:

Parsing JSON feed automatically into MS Access的可能重复 我的 json 有这个标签 "total": 140, "pages": 28, "this_page": 1, "reviews": [ 我需要遍历数据 还有:***.com/questions/6627652/parsing-json-in-excel-vba 【参考方案1】:
Sub ParseJson()

    Dim j As String, sc As Object, num As Long, i

    j = Range("A1").Value 'I'm testing in excel...

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

    'evaluate the json to create a "response" object
    sc.ExecuteStatement "var response = eval((" & j & "));"

    'create a function to return us a piece of data
    '   based on the passed-in js fragment
    sc.ExecuteStatement "var f = function(s)return eval(s);;"

    'how many reviews ?
    num = sc.Eval("f('response.reviews.length')") '>> 4

    For i = 0 To num - 1
        Debug.Print "------- Review " & i & "---------------"
        Debug.Print sc.Eval("f('response.reviews[" & i & "].author')")
        Debug.Print sc.Eval("f('response.reviews[" & i & "].title')")
        'etc etc
    Next i

End Sub

【讨论】:

我实际上是连接到一个 api 来获取这些数据。我已经在 VBA 中找到了连接部分。解析数据有点问题。我认为蒂姆正在引导我找到解决方案

以上是关于访问VBA解析json的主要内容,如果未能解决你的问题,请参考以下文章

VBA中使用JavaScript脚本语言解析JSON数据

是否有适用于 VB6/VBA 的 JSON 解析器?

VBA研究64位系统下无法用ScriptControl控件解析JSon数据

Swift JSON 解析访问数组

如何解析 JSON 并访问结果

快速解析双 JSON 编码数组并访问它