所需对象:循环访问JSON数据时出现'[undefined]'错误

时间:2022-06-17 05:10:08

I am working with the Google Translation API, which returns results in JSON format - e.g.

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

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

I am trying to parse the JSON data using Classic ASP.

我试图使用经典ASP解析JSON数据。

I'm using ASPJSON (http://www.aspjson.com/) to parse the JSON data.

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

I can get so far with reading the data - e.g. (where "BackFromGoogle") is the objXML.responseText from a MSXML2.ServerXMLHTTP call.

到目前为止,我可以阅读数据 - 例如(其中“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    

If I then try:

如果我然后尝试:

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    

Then I get this error:

然后我收到这个错误:

Microsoft VBScript runtime error '800a01a8' Object required: '[undefined]'

Microsoft VBScript运行时错误'800a01a8'对象必需:'[undefined]'

For this line:

对于这一行:

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

I am very stuck working out the syntax to allow me to access the individual values of the "translatedText" lines.

我很难找到允许我访问“translatedText”行的各个值的语法。

Is it possible to access them?

是否可以访问它们?

1 个解决方案

#1


2  

Got this working in the end.

最后有这个工作。

Found the answer via the solution here: VbScript Deserialize JSON

通过这里的解决方案找到答案:VbScript反序列化JSON

This sorted it:

这排序了:

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

#1


2  

Got this working in the end.

最后有这个工作。

Found the answer via the solution here: VbScript Deserialize JSON

通过这里的解决方案找到答案:VbScript反序列化JSON

This sorted it:

这排序了:

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