asp读取解析JSON的几种方法

转载 2015年11月21日 01:51:51

方法一(使用MSScriptControl.ScriptControl):

<%
Dim sc4Json,arr0,json
InitScriptControl

json="{""name"":""123"",""content"":[{""id"":""1""},{""id"":""2""}]}"

Set jsonobj=getJSONObject(json)
'方法一
getJSArrayItem arr0,jsonobj.content,0
response.write jsonobj.name&"<br/>"&arr0.id&"<br/>"
'方法二
response.write sc4Json.Eval("jsonObject.name")&"<br/>"
response.write sc4Json.Eval("jsonObject.content[1].id")

Sub InitScriptControl
Set sc4Json = Server.CreateObject("MSScriptControl.ScriptControl")
sc4Json.Language = "JavaScript"
sc4Json.AddCode "var itemTemp=null;function getJSArray(arr, index){itemTemp=arr[index];}"
End Sub
Function getJSONObject( strJSON )
sc4Json.AddCode "var jsonObject = " & strJSON
Set getJSONObject = sc4Json.CodeObject.jsonObject
End Function
Sub getJSArrayItem( objDest, objJSArray, index )
On Error Resume Next
sc4Json.Run "getJSArray",objJSArray, index
Set objDest = sc4Json.CodeObject.itemTemp
If Err.number=0 Then  Exit Sub End If
objDest = sc4Json.CodeObject.itemTemp
End Sub%>

方法二:

<%
Dim sc4
Sub InitScriptControl
Set sc4Json = Server.CreateObject(“MSScriptControl.ScriptControl”)
   sc4Json.Language = “JavaScript”
   sc4Json.AddCode “var itemTemp=null;function getJSArray(arr, index){itemTemp=arr[index];}”
End Sub

Function getJSONObject(strJSON)
   sc4Json.AddCode “var jsonObject = ” & strJSON
   Set getJSONObject = sc4Json.CodeObject.jsonObject
End Function

Sub getJSArrayItem(objDest,objJSArray,index)
   On Error Resume Next
   sc4Json.Run “getJSArray”,objJSArray, index
   Set objDest = sc4Json.CodeObject.itemTemp
   If Err.number=0 Then Exit Sub
   objDest = sc4Json.CodeObject.itemTemp
End Sub

Dim strTest
strTest = “{name:”"alonely”", age:24, email:[""ycplxl1314@163.com"",""ycplxl1314@gmail.com""], family:{parents:[""父亲"",""母亲""],toString:function(){return “”家庭成员”";}}}”
Dim objTest
Call InitScriptControl
Set objTest = getJSONObject(strTest)
%>
<%=objTest.name%& gt;的邮件地址是<%=sc4Json.eval_r(“jsonObject.email[0]“)%><BR>共有邮件地址& lt;%=objTest.email.length%>个<BR>
<%
Dim father
getJSArrayItem father, objTest.family.parents, 0
Response.Write father
%>


方法三(类库):

<%

‘    VBS JSON 2.0.3
‘    Copyright (c) 2009
‘    Under the MIT (MIT-LICENSE.txt) license.

Const JSON_OBJECT    = 0
Const JSON_ARRAY    = 1

Class jsCore
Public Collection
Public Count
Public QuotedVars
Public Kind ‘ 0 = object, 1 = array

Private Sub Class_Initialize
Set Collection = CreateObject(“Scripting.Dictionary”)
QuotedVars = True
Count = 0
End Sub

Private Sub Class_Terminate
Set Collection = Nothing
End Sub

‘ counter
Private Property Get Counter
Counter = Count
Count = Count + 1
End Property

‘ – data maluplation
‘ — pair
Public Property Let Pair(p, v)
If IsNull(p) Then p = Counter
Collection(p) = v
End Property

Public Property Set Pair(p, v)
If IsNull(p) Then p = Counter
If TypeName(v) <> “jsCore” Then
Err.Raise &hD, “class: class”, “Incompatible types: ‘” & TypeName(v) & “‘”
End If
Set Collection(p) = v
End Property

Public Default Property Get Pair(p)
If IsNull(p) Then p = Count – 1
If IsObject(Collection(p)) Then
Set Pair = Collection(p)
Else
Pair = Collection(p)
End If
End Property
‘ — pair
Public Sub Clean
Collection.RemoveAll
End Sub

Public Sub Remove(vProp)
Collection.Remove vProp
End Sub
‘ data maluplation

‘ encoding
Function jsEncode(str)
Dim charmap(127), haystack()
charmap(8)  = “\b”
charmap(9)  = “\t”
charmap(10) = “\n”
charmap(12) = “\f”
charmap(13) = “\r”
charmap(34) = “\”"”
charmap(47) = “\/”
charmap(92) = “\\”

Dim strlen : strlen = Len(str) – 1
ReDim haystack(strlen)

Dim i, charcode
For i = 0 To strlen
haystack(i) = Mid(str, i + 1, 1)

charcode = AscW(haystack(i)) And 65535
If charcode < 127 Then
If Not IsEmpty(charmap(charcode)) Then
haystack(i) = charmap(charcode)
ElseIf charcode < 32 Then
haystack(i) = “\u” & Right(“000″ & Hex(charcode), 4)
End If
Else
haystack(i) = “\u” & Right(“000″ & Hex(charcode), 4)
End If
Next

jsEncode = Join(haystack, “”)
End Function

‘ converting
Public Function toJSON(vPair)
Select Case VarType(vPair)
Case 0    ’ Empty
toJSON = “null”
Case 1    ’ Null
toJSON = “null”
Case 7    ’ Date
‘ toJSON = “new Date(” & (vPair – CDate(25569)) * 86400000 & “)”    ’ let in only utc time
toJSON = “”"” & CStr(vPair) & “”"”
Case 8    ’ String
toJSON = “”"” & jsEncode(vPair) & “”"”
Case 9    ’ Object
Dim bFI,i
bFI = True
If vPair.Kind Then toJSON = toJSON & “[" Else toJSON = toJSON & "{"
For Each i In vPair.Collection
If bFI Then bFI = False Else toJSON = toJSON & ","

If vPair.Kind Then
toJSON = toJSON & toJSON(vPair(i))
Else
If QuotedVars Then
toJSON = toJSON & """" & i & """:" & toJSON(vPair(i))
Else
toJSON = toJSON & i & ":" & toJSON(vPair(i))
End If
End If
Next
If vPair.Kind Then toJSON = toJSON & "]” Else toJSON = toJSON & “}”
Case 11
If vPair Then toJSON = “true” Else toJSON = “false”
Case 12, 8192, 8204
toJSON = RenderArray(vPair, 1, “”)
Case Else
toJSON = Replace(vPair, “,”, “.”)
End select
End Function

Function RenderArray(arr, depth, parent)
Dim first : first = LBound(arr, depth)
Dim last : last = UBound(arr, depth)

Dim index, rendered
Dim limiter : limiter = “,”

RenderArray = “["
For index = first To last
If index = last Then
limiter = ""
End If

On Error Resume Next
rendered = RenderArray(arr, depth + 1, parent & index & "," )

If Err = 9 Then
On Error GoTo 0
RenderArray = RenderArray & toJSON(Eval("arr(" & parent & index & ")")) & limiter
Else
RenderArray = RenderArray & rendered & "" & limiter
End If
Next
RenderArray = RenderArray & "]”
End Function

Public Property Get jsString
jsString = toJSON(Me)
End Property

Sub Flush
If TypeName(Response) <> “Empty” Then
Response.Write(jsString)
ElseIf WScript <> Empty Then
WScript.Echo(jsString)
End If
End Sub

Public Function Clone
Set Clone = ColClone(Me)
End Function

Private Function ColClone(core)
Dim jsc, i
Set jsc = new jsCore
jsc.Kind = core.Kind
For Each i In core.Collection
If IsObject(core(i)) Then
Set jsc(i) = ColClone(core(i))
Else
jsc(i) = core(i)
End If
Next
Set ColClone = jsc
End Function

End Class

Function jsObject
Set jsObject = new jsCore
jsObject.Kind = JSON_OBJECT
End Function

Function jsArray
Set jsArray = new jsCore
jsArray.Kind = JSON_ARRAY
End Function

Function toJSON(val)
toJSON = (new jsCore).toJSON(val)
End Function
%>

类库提供了两种js类型,object和array

使用方法如下:

Set hash = jsObject() ‘如果要输出array就写Set hash=jsArray()
hash(“error”) = 0
hash(“url”) = fileUrl
hash.Flush
Response.End

输出的就是json格式的字符串了


相关文章推荐

c#在Asp.Net中解析json数据

来自:http://outofmemory.cn/code-snippet/1466/c-Asp-Net-jiexi-json-data  在.Net framework 3.5之后有了Sy...

c#中的Ajax上手篇---JSON的传送与接收

本篇文章主要介绍如何在Ajax的客户端与服务端之间传送JSON格式的数据。那么我们先来说说什么是JSON吧。JSON,即是JavaScript对象标记法,英文名称是JavaScript Object ...

ASP.net解析JSON例子

using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste...

asp+jquery+ajax+json实例

asp: 层    次: 全部 "> 专    业: 全部 <% sql="select distinct subject from

关于asp输出json对象的方法及实例

相信大家对于asp和json都不陌生,当下json应用的非常广泛,但asp语言中并没有可以直接生成json对象的方法,我在网上查了很多资料,关于asp生成json对象的方法,大多都写得很简单,大多很差...

Asp中JSON的使用

我对asp完全不懂,由于需要使用json的结构,就研究了一下asp的json的使用,拼接一个json的字符串。测试用例中使用到了一个lib文件:Json_2.0.3.asp <% ' ' ...

ASP解析JSON例子

JSON(Javascript Object Notation) 我想这个名字大家已经不陌生了,它是一种轻量级的数据交换格式,是由服务直接生成Javascript语句,客户端获取后直接用eval方法来...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)