asp解析复杂json数据实例

首页 - 技术杂谈 本文发布于2016-10-25 11:45:00

假设用asp要解析以下json数据:

{"cid": "guabu.com","tripType": "3","sessionId": "204","routing": {"data": "5589a6d9d291930cbdb5c19a043f22e3","fromSegments": [{"carrier": "D7","flightNumber": "D7505","depAirport": "SEL","depTime": "201605071625","arrAirport": "KUL","arrTime": "201605072155","stopCities": "","codeShare": false,"codeshare": false,"cabin": "E","aircraftCode": "","cabinClass": 1},{"carrier": "AK","flightNumber": "AK701","depAirport": "KUL","depTime": "201605080610","arrAirport": "SIN","arrTime": "201605080715","stopCities": "","codeShare": false,"codeshare": false,"cabin": "E","aircraftCode": "","cabinClass": 1}],"retSegments": []},"passengers": [{"name": "GUA/BU","gender": "M","ageType": 0,"birthday": "19920202","nationality": "CN","cardType": "PP","cardNum": "G77777777","cardExpired": "20250725","cardIssuePlace": "CN"},{"name": "BU/GUA","gender": "M","ageType": 1,"birthday": "20050202","nationality": "CN","cardType": "PP","cardNum": "G77777770","cardExpired": "20250725","cardIssuePlace": "CN"}],"contact": {"name": "CESHI/DAN","address": "bj,china","postcode": "100000","email": "bd@guabu.com","mobile": "12345678"}}

代码运行地址页:http://www.guabu.com/jishuzatan/aspschool/json.asp

建立一个json.asp页面,示例代码如下:

<%@ CODEPAGE=65001 %><% Response.CodePage=65001%><% Response.Charset="UTF-8" %>
<%
Server.ScriptTimeOut = 60'60秒须返回
Set rs = Server.CreateObject("ADODB.Recordset")
'json = "{""cid"": ""guabu.com"",""tripType"": ""1"",""sessionId"": ""108"",""routing"": {""data"": ""6b4efeaf395a955fe0769ad3421f58cc"",""fromSegments"": [{""carrier"": ""AK"",""flightNumber"": ""AK822"",""depAirport"": ""KUL"",""depTime"": ""201604300730"",""arrAirport"": ""HKT"",""arrTime"": ""201604300855"",""stopCities"": """",""codeShare"": false,""codeshare"": false,""cabin"": ""E"",""aircraftCode"": """",""cabinClass"": 1}],""retSegments"": []},""passengers"": [{""name"": ""GUA/BU"",""gender"": ""M"",""ageType"": 0,""birthday"": ""19920202"",""nationality"": ""CN"",""cardType"": ""PP"",""cardNum"": ""G77777777"",""cardExpired"": ""20250725"",""cardIssuePlace"": ""CN""},{""name"": ""SONG/MINGLI"",""gender"": ""M"",""ageType"": 1,""birthday"": ""20050202"",""nationality"": ""CN"",""cardType"": ""PP"",""cardNum"": ""G77777770"",""cardExpired"": ""20250725"",""cardIssuePlace"": ""CN""}],""contact"": {""name"": ""CESHI/DAN"",""address"": ""bj,china"",""postcode"": ""100000"",""email"": ""bd@guabu.com"",""mobile"": ""12345678""}}"	'单程
json = "{""cid"": ""guabu.com"",""tripType"": ""3"",""sessionId"": ""204"",""routing"": {""data"": ""5589a6d9d291930cbdb5c19a043f22e3"",""fromSegments"": [{""carrier"": ""D7"",""flightNumber"": ""D7505"",""depAirport"": ""SEL"",""depTime"": ""201605071625"",""arrAirport"": ""KUL"",""arrTime"": ""201605072155"",""stopCities"": """",""codeShare"": false,""codeshare"": false,""cabin"": ""E"",""aircraftCode"": """",""cabinClass"": 1},{""carrier"": ""AK"",""flightNumber"": ""AK701"",""depAirport"": ""KUL"",""depTime"": ""201605080610"",""arrAirport"": ""SIN"",""arrTime"": ""201605080715"",""stopCities"": """",""codeShare"": false,""codeshare"": false,""cabin"": ""E"",""aircraftCode"": """",""cabinClass"": 1}],""retSegments"": []},""passengers"": [{""name"": ""GUA/BU"",""gender"": ""M"",""ageType"": 0,""birthday"": ""19920202"",""nationality"": ""CN"",""cardType"": ""PP"",""cardNum"": ""G77777777"",""cardExpired"": ""20250725"",""cardIssuePlace"": ""CN""},{""name"": ""BU/GUA"",""gender"": ""M"",""ageType"": 1,""birthday"": ""20050202"",""nationality"": ""CN"",""cardType"": ""PP"",""cardNum"": ""G77777770"",""cardExpired"": ""20250725"",""cardIssuePlace"": ""CN""}],""contact"": {""name"": ""CESHI/DAN"",""address"": ""bj,china"",""postcode"": ""100000"",""email"": ""bd@guabu.com"",""mobile"": ""12345678""}}"	'多程

response.Write(json)'要解析的json数据
response.Write("<br>解析结果如下:<br>")

Dim scriptCtrl	'必须得有此项定义
Function parseJSON(str)
If Not IsObject(scriptCtrl) Then
Set scriptCtrl = Server.CreateObject("MSScriptControl.ScriptControl")
scriptCtrl.Language = "JScript"
scriptCtrl.AddCode "function ActiveXObject() {}" ' 覆盖 ActiveXObject
scriptCtrl.AddCode "function GetObject() {}" ' 覆盖 ActiveXObject
scriptCtrl.AddCode "Array.prototype.get = function(x) { return this[x];}; var result = null;"
End If
  On Error Resume Next
scriptCtrl.ExecuteStatement "result = " & str & ";"
Set parseJSON = scriptCtrl.CodeObject.result
  If Err Then
Err.Clear
Set parseJSON = Nothing
  End If
End Function
 
Set obj = parseJSON(json)

cid =  obj.cid	'活得节点cid的值
tripType = obj.tripType	'注意tripTpye和tripType的区别
session_id = obj.sessionId

data = obj.routing.data	'获得节点routing下的data值
Lianxiren = obj.contact.name	'获得节点contact下的name值
address = obj.contact.address
postcode = obj.contact.postcode
email = obj.contact.email
Tel = obj.contact.mobile

response.Write("cid:"&cid&"<br>")
response.Write("tripType:"&tripType&"<br>")
response.Write("session_id:"&session_id&"<br>")
response.Write("Lianxiren:"&Lianxiren&"<br>")
response.Write("data:"&data&"<br>")
response.Write("address:"&address&"<br>")
response.Write("postcode:"&postcode&"<br>")
response.Write("email:"&email&"<br>")
response.Write("Tel:"&Tel&"<br>")

'循环读取行程段
for i=0 to obj.routing.fromSegments.length-1	'行程段
carrier = obj.routing.fromSegments.get(i).carrier
depAirport = obj.routing.fromSegments.get(i).depAirport
depTime = obj.routing.fromSegments.get(i).depTime
arrAirport = obj.routing.fromSegments.get(i).arrAirport
arrTime = obj.routing.fromSegments.get(i).arrTime
stopCities = obj.routing.fromSegments.get(i).stopCities
codeShare = LCase(obj.routing.fromSegments.get(i).codeShare)
cabin = obj.routing.fromSegments.get(i).cabin
aircraftCode = obj.routing.fromSegments.get(i).aircraftCode
flightNumber = obj.routing.fromSegments.get(i).flightNumber
cabinClass = obj.routing.fromSegments.get(i).cabinClass

response.Write("<br>"&carrier&"<br>")
response.Write(depAirport&"<br>")
response.Write(depTime&"<br>")
response.Write(arrAirport&"<br>")
response.Write(arrTime&"<br>")
response.Write(stopCities&"<br>")
response.Write(cardIssuePlace&"<br>")
response.Write(codeShare&"<br>")
response.Write(cabin&"<br>")
response.Write(aircraftCode&"<br>")
response.Write(flightNumber&"<br>")
response.Write(cabinClass&"<br>")
next
'循环读取订单信息
for m=0 to obj.passengers.length-1	'订单人数
username = obj.passengers.get(m).name
ageType = obj.passengers.get(m).ageType
birthday = obj.passengers.get(m).birthday
gender = obj.passengers.get(m).gender
cardNum = obj.passengers.get(m).cardType
cardType = obj.passengers.get(m).cardNum
cardIssuePlace = obj.passengers.get(m).cardExpired
cardExpired = obj.passengers.get(m).cardIssuePlace
nationality = obj.passengers.get(m).nationality

response.Write("<br>"&username&"<br>")
response.Write(ageType&"<br>")
response.Write(birthday&"<br>")
response.Write(gender&"<br>")
response.Write(cardNum&"<br>")
response.Write(cardType&"<br>")
response.Write(cardIssuePlace&"<br>")
response.Write(cardExpired&"<br>")
response.Write(nationality&"<br>")
next

Set obj = Nothing
Set scriptCtrl = Nothing  
%>


首页 | 吉凶大全 | 星座配对 | 塔罗牌

提示:本站不提供技术解答服务