为什么我不能为变量分配在调试模式下可以看到的JSON值? [英] Why can't I assign to a variable a JSON value that I can see in Debug mode?

查看:69
本文介绍了为什么我不能为变量分配在调试模式下可以看到的JSON值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了从非项目化JSON对象检索值的问题.我认为这样做很简单……只需使用所需字段(例如JSON("title"))引用对象即可.但是即使有值,我也无法检索.

I’ve run into an issue retrieving values from a non-itemized JSON object. I thought it was simple do so … Just reference the object with the field you want (e.g. JSON("title"). But I cannot retrieve a value even though it IS there.

此代码演示了我在说什么. (请确保在下一条"行上放置一个断点,否则它将运行一会儿.)未分配strID和strTitle值,也不会打印出来.但是,如果您进入立即窗口并输入

This code demonstrates what I’m talking about. (Be sure to put a breakpoint on the "next" line, or it will run for a while.) The strID and strTitle values are not assigned and do not print out. But if you go to the immediate window and type

? JSON2("ID") ? JOON2(标题")

? JSON2("ID") ? JOON2("title")

您将获得这些值.我究竟做错了什么?为什么不能将这些值转换为变量?

You get the values. What am I doing wrong? Why can’t I get these values into variables?

Sub testMovie2()

Dim Url As String, data As String, data2 As String
Dim xml As Object, JSON As Object, JSON2 As Object, colObj As Object, colobj2 As Object, item, item2
Dim strID As String, strTitle As String

Url = "https://www.tiff.net/data/films-events-2018.json"
data = getHTTP(Url)
Set JSON = JsonConverter.ParseJson(data)
Set colObj = JSON("items")
For Each item In colObj
    Url = "https://www.tiff.net/data/films/" & item("id") & ".JSON"
    data2 = getHTTP(Url)
    Set JSON2 = JsonConverter.ParseJson(data2)
    strID = JSON2("ID")
    Debug.Print strID
    strTitle = JSON2("Title")
    Debug.Print strTitle
Next
End Sub

推荐答案

JSON2是字典对象,并从下面的字典使用中检索元素

JSON2 is a dictonary object and to retrieve element from dictonary use below

带键

JSON2.item("id")
JSON2.item("title")

OR

带有索引

JSON2.Items()(4)
JSON2.Items()(5)

默认情况下,字典对象区分大小写

By default dictionary objects are case sensitive

所以JSON2("ID")不等于JSON2("id")

要使其不区分大小写,请使用:

To make it case insensitive use:

JSON2.CompareMode = vbTextCompare

代码:

Sub testMovie2()

    Dim url As String, data As String, data2 As String
    Dim xml As Object, JSON As Object, JSON2 As Object, colObj As Object, colobj2 As Object, item, item2
    Dim strID As String, strTitle As String

    url = "https://www.tiff.net/data/films-events-2018.json"
    data = getHTTP(url)
    Set JSON = JsonConverter.ParseJson(data)
    Set colObj = JSON("items")
    For Each item In colObj
        url = "https://www.tiff.net/data/films/" & item("id") & ".JSON"
        data2 = getHTTP(url)

        Set JSON2 = JsonConverter.ParseJson(data2)
        strID = JSON2.item("id")
        Debug.Print strID

        strTitle = JSON2.item("title")
        Debug.Print strTitle

    Next
End Sub
Function getHTTP(url) As String

    Dim data As String
    Dim xml As Object

    Set xml = CreateObject("MSXML2.ServerXMLHTTP")
    With xml
        .Open "GET", url, False
        .setRequestHeader "Content-Type", "text/json"
        .send
        data = .responseText
    End With

    getHTTP = data

End Function



这篇关于为什么我不能为变量分配在调试模式下可以看到的JSON值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆