【问题标题】:How do you retrieve a JSON property from an array in aspJSON?如何从 aspJSON 中的数组中检索 JSON 属性?
【发布时间】:2021-12-29 15:50:30
【问题描述】:
testdata =
"{payments:
  [{'status': 'succeeded', 
    'in_store_payment_type': None, 
    'refunds': [], 
    'created_at': '2021-10-20T17:23:41-0400', 
    'amount_paid_display': '53.00', 
    'initial_amount_paid_display': '53.00', 
    'currency': 'usd', 
    'initial_amount_paid': 5300, 
    'pk': 75552720, 
    'type': 'affiliate', 
    'amount_paid': 5300
  }]
}"

我试过了:

<%@ LANGUAGE="VBSCRIPT" %>  
<!--#include file="aspJSON.asp" -->
<%
(read testdata....)

set oJSON= json.parse(testdata)

response.write "Data->" & oJSON.payments.get(0).status
%>

曾希望看到“成功”但没有运气。
有什么建议吗?

【问题讨论】:

  • 这能回答你的问题吗? How to access JSON data in classic ASP using json2.asp or aspjson libraries?(好像你已经删除了other question)。
  • 嗯,我认为它确实可以,但是我无法让它与我的数据一起使用
  • 您提供的示例看起来不像 Classic ASP 的正确语法,#include 的结构不正确,VBScript 应该包含在&lt;% ... %&gt; ASP 预处理器标记中。这永远不会运行,您一定会遇到错误,您实际上可以edit 问题并发布错误是什么或至少可以运行示例吗?
  • 您提供的数据不是 JSON。您必须手动解析它。
  • 标识符的单引号 (') 在 JSON 对象中无效,您应该使用双引号 (")。见Introducing JSON

标签: arrays json vbscript asp-classic


【解决方案1】:

此脚本存在一些问题,个人不是 aspJSON library 的粉丝,但这应该可以帮助您入门(更改 #include 位置以适应您的环境)。

<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<!-- #include virtual = "/scripts/aspJSON1.19.asp" -->
<%
Dim testdata: testdata = _
  "{""payments"" :" & _
  "  [{""status"": ""succeeded""," & _
  "   ""in_store_payment_type"": None," & _ 
  "   ""refunds"": []," & _
  "   ""created_at"": ""2021-10-20T17:23:41-0400""," & _
  "   ""amount_paid_display"": ""53.00""," & _
  "   ""initial_amount_paid_display"": ""53.00""," & _
  "   ""currency"": ""usd""," & _
  "   ""initial_amount_paid"": 5300," & _
  "   ""pk"": 75552720," & _
  "   ""type"": ""affiliate""," & _
  "   ""amount_paid"": 5300" & _
  " }]" & _
  "}"

Dim oJSON: Set oJSON = New aspJSON
Call oJSON.loadJSON(testdata)
Dim payment
For Each payment In oJSON.data("payments")
  Dim this: Set this = oJSON.data("payments")(payment)
  response.write "Data->" & this.item("status")
Next
%>

输出:

Data->succeeded

几个关键点

  • JSON 的标识符需要使用双引号 (") 而不是单引号 (')。参考Introducing JSON
  • aspJSON 库不支持parse() 方法,而是使用loadJSON()
  • 您需要使用For 语句枚举payments 集合,以访问枚举对象内的属性。

有用的链接

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多