VbScript反序列化JSON [英] VbScript Deserialize JSON
问题描述
使用"ASPJSON"库- http://www.aspjson.com/
Using the "ASPJSON" library - http://www.aspjson.com/
我正在尝试获取以下JSON字符串中的"emailAddress"值
I am attempting to get to the "emailAddress" value in the following JSON string
{
"result": {
"lead": [
{
"id": "1",
"emailAddress": "mickey@mouse.com"
},
{
"id": "2",
"emailAddress": "mini@mouse.com"
}
]
},
"error": null,
"id": "1"
}
我在使用循环获取价值时遇到了问题,我认为这是唯一的方法,可以倒出2个电子邮件地址.
I am having problems getting to the value using loops, which I believe is the only way to pul out the 2 email addresses.
这是我到目前为止的代码:
This is the code I have so far:
For Each result In oJSON.data("result")
Set this = oJSON.data("result")
For Each lead in this("lead")
Set this2 = oJSON.data("active").item(lead)
response.Write("lead") & "<br>"
Next
Next
当我赞扬台词时
Set this2 = oJSON.data("active").item(lead)
我在响应中写了2行.写,所以我相信我已经达到了正确的水平"
I get 2 lines written by the response.Write so I believe I have got down to the right "level"
但是我尝试提取值失败.
But I have failed trying to pull out the values.
库作者给出的示例如下:
The example given by the author of the library looks like this:
但是我无法让它做我需要做的事情:
But I have been unable to get it to do what I need:
示例JSON:
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25,
"address" :
{
"streetAddress": "21 2nd Street",
"city" : "New York",
"state" : "NY",
"postalCode" : "10021"
},
"phoneNumber":
[
{
"type" : "home",
"number": "212 555-1234"
},
{
"type" : "fax",
"number": "646 555-4567"
}
]
}
代码示例为:
<!--#include virtual="/aspJSON1.17.asp" -->
<%
Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON(jsonstring)
'Get single value
Response.Write oJSON.data("firstName") & "<br>"
'Loop through collection
For Each phonenr In oJSON.data("phoneNumber")
Set this = oJSON.data("phoneNumber").item(phonenr)
Response.Write _
this.item("type") & ": " & _
this.item("number") & "<br>"
Next
'Update/Add value
oJSON.data("firstName") = "James"
'Return json string
Response.Write oJSON.JSONoutput()
%>
推荐答案
终于使它起作用了!
For Each result In oJSON.data("result")("lead")
Set this = oJSON.data("result")("lead").item(result)
response.Write this.item("emailAddress") & "<br>"
Next
这篇关于VbScript反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!