ASPJson和Mandrill API

时间:2014-01-12 22:15:57

标签: json api asp-classic

我正在使用aspJSON类: http://www.aspjson.com/

尝试编写一些简单的JSON用于Mandrill电子邮件API的基本测试: https://mandrillapp.com/api/docs/messages.JSON.html#method=send

我通过这个基本测试使用aspJSON页面中的“write.asp”示例写了一小段JSON:

<!--#include file="aspJSON.asp" -->
<%
Set oJSON = New aspJSON

'Write single value
oJSON.data("familyName") = "Smith"

'Make collection
Set oJSON.data("familyMembers") = oJSON.Collection()

'Add instances to collection
Set newitem = oJSON.AddToCollection(oJSON.data("familyMembers"))
newitem.add "firstName", "John"
newitem.add "age", 41
newitem.add "gender", "Male"

Set newitem = oJSON.AddToCollection(oJSON.data("familyMembers"))
newitem.add "firstName", "Suzan"
newitem.add "age", 38
newitem.add "gender", "Female"

Set newitem = oJSON.AddToCollection(oJSON.data("familyMembers"))
newitem.add "firstName", "John Jr."
newitem.add "age", 11
newitem.add "gender", "Male"

'Return the object
Response.Write oJSON.JSONoutput()
%>

这适用于非常“扁平”的JSON结构,如下所示:

{
  "familyName": "Smith",
  "familyMembers":
  [
    {
      "firstName": "John",
      "age": 41,
      "gender": "Male"
    },
    {
      "firstName": "Suzan",
      "age": 38,
      "gender": "Female"
    },
    {
      "firstName": "John Jr.",
      "age": 11,
      "gender": "Male"
    }
  ]
}

但我正在努力的是如何编写一个JSON字符串,如Madrill文档中所示,其中嵌套比aspJSON示例中显示的更深。

例如,这是Mandrill JSON字符串的摘录:

{
    "key": "example key",
    "message": {
        "html": "<p>Example HTML content</p>",
        "text": "Example text content",
        "subject": "example subject",
        "from_email": "message.from_email@example.com",
        "from_name": "Example Name",
        "to": [
            {
                "email": "recipient.email@example.com",
                "name": "Recipient Name",
                "type": "to"
            }
        ],
        "headers": {
            "Reply-To": "message.reply@example.com"
        },
    "async": false,
    "ip_pool": "Main Pool",
    "send_at": "example send_at"
}

正如您在“to”和“headers”部分所看到的,嵌套级别比aspJSON示例更深。

我在我的ASP代码中试过这个:

<!--#include file="aspJSON.asp" -->
<%
Set oJSON = New aspJSON

'Write single value
oJSON.data("key") = "MY_KEY"

'Make collection
Set oJSON.data("message") = oJSON.Collection()

'Add instances to collection
Set newitem = oJSON.AddToCollection(oJSON.data("message"))
newitem.add "text", "Hello world!"
newitem.add "subject", "Test Subject"
newitem.add "from_email", "me@you.com"
newitem.add "from_name", "Bob Holnas"

Set newitem = oJSON.AddToCollection(oJSON.data("to"))
newitem.add "email", "him@her.com"
newitem.add "name", "Arthur Smith"
newitem.add "type", "to"

Set newitem = oJSON.AddToCollection(oJSON.data("headers"))
newitem.add "Reply-To", "us@them.com"

'Return the object
Response.Write oJSON.JSONoutput()
%>

但得到了这个错误:

AddToCollection Error error '800a0001'
Not a collection.
/websites/aspJSON/aspJSON.asp, line 77

我在我的网站上本地托管了“aspJSON.asp”中的数据: http://jimpix.co.uk/aspJSON.txt

因为它不能通过aspJSON网站获得。

任何建议都会非常感激,因为我真的被卡住了!

由于

1 个答案:

答案 0 :(得分:3)

首先让我说我以前从未使用过这门课 在读完课程后,我写了一个例子并试图在评论中提供帮助 希望能帮助到你。

<!--#include file="aspJSON.asp" -->
<%
Set oJSON = New aspJSON

With oJSON.data
    .Add "key", "MY_KEY"
    .Add "message", oJSON.Collection()
    With oJSON.data("message")
        .Add "text", "Hello world!"
        .Add "subject", "Test Subject"
        .Add "from_email", "me@you.com"
        .Add "from_name", "Bob Holnas"
        .Add "to", oJSON.Collection()
        With .Item("to")
            'To obtain a collection will be considered an array for output, specify integer keys instead of string
            .Add 0, oJSON.Collection() 'first index is a collection
            With .Item(0) 'add key-value pairs to first index of the collection
                .Add "email", "him@her.com"
                .Add "name", "Arthur Smith"
                .Add "type", "to"
            End With
        End With
        .Add "headers", oJSON.Collection()
        With .Item("headers")
            .Add "Reply-To", "us@them.com"
        End With
    End With
    '.Add "async", false
    '.Add "ip_pool", "Main Pool"
    '.Add "send_at", "example send_at"
End With    

Response.Write oJSON.JSONoutput()
%>

给定输出:

{
    "key": "MY_KEY",
    "message": {
        "text": "Hello world!",
        "subject": "Test Subject",
        "from_email": "me@you.com",
        "from_name": "Bob Holnas",
        "to": [
            {
                "email": "him@her.com",
                "name": "Arthur Smith",
                "type": "to"
            }
        ],
        "headers": {
            "Reply-To": "us@them.com"
        }
    }
}