【问题标题】:Object required: '[undefined]' error when looping through JSON data需要对象:循环 JSON 数据时出现“[未定义]”错误
【发布时间】:2015-11-21 09:03:55
【问题描述】:

我正在使用 Google Translation API,它以 JSON 格式返回结果 - 例如

{
 "data": {
  "translations": [
   {
    "translatedText": "Hola mundo"
   },
   {
    "translatedText": "Te amo"
   },
   {
    "translatedText": "queso"
   }
  ]
 }
}

我正在尝试使用 Classic ASP 解析 JSON 数据。

我正在使用 ASPJSON (http://www.aspjson.com/) 来解析 JSON 数据。

到目前为止,我可以读取数据 - 例如(其中“BackFromGoogle”)是来自 MSXML2.ServerXMLHTTP 调用的 objXML.responseText。

Set oJSON = New aspJSON

oJSON.loadJSON(BackFromGoogle)

For Each translation In oJSON.data("data") 'iterate through data
    Set this = oJSON.data("data").item(translation)
Next    

如果我再尝试:

For Each translation In oJSON.data("data") 'iterate through data
    Set this = oJSON.data("data").item(translation)
    Response.Write this.item("translations").item("translatedText")
Next    

然后我得到这个错误:

Microsoft VBScript 运行时错误“800a01a8” 需要对象:'[未定义]'

对于这一行:

Response.Write this.item("translations").item("translatedText")

我非常坚持研究语法以允许我访问“translatedText”行的各个值。

是否可以访问它们?

【问题讨论】:

    标签: json asp-classic


    【解决方案1】:

    最后搞定了。

    通过这里的解决方案找到答案: VbScript Deserialize JSON

    这样排序的:

    Set oJSON = New aspJSON
    
    oJSON.loadJSON(BackFromGoogle)
    
    For Each result In oJSON.data("data")("translations")
    Set this = oJSON.data("data")("translations").item(result)
            response.Write this.item("translatedText") & "<br>"
    Next
    

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 2022-07-20
      • 2019-01-22
      • 2019-01-13
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多