在经典ASP中解析JSON有什么好的库吗?

时间:2022-11-07 01:46:29

I've been able to find a zillion libraries for generating JSON in Classic ASP (VBScript) but I haven't been to find ANY for parsing.

我已经找到了大量在经典ASP (VBScript)中生成JSON的库,但是还没有找到任何解析的库。

I want something that I can pass a JSON string and get back a VBScript object of some sort (Array, Scripting.Dictionary, etc)

我想要一些东西,我可以传递JSON字符串并返回某种类型的VBScript对象(数组,脚本)。字典等)

Can anyone recommend a library for parsing JSON in Classic ASP?

8 个解决方案

#1


83  

Keep in mind that Classic ASP includes JScript as well as VBScript. Interestingly, you can parse JSON using JScript and use the resulting objects directly in VBScript.

记住,经典的ASP包括JScript和VBScript。有趣的是,您可以使用JScript解析JSON并直接在VBScript中使用结果对象。

Therefore, it is possible to use the canonical https://github.com/douglascrockford/JSON-js/blob/master/json2.js in server-side code with zero modifications.

因此,可以在服务器端代码中使用规范的https://github.com/douglas douglas crock/json -js/blob/master/json2.js,无需修改。

Of course, if your JSON includes any arrays, these will remain JScript arrays when parsing is complete. You can access the contents of the JScript array from VBScript using dot notation.

当然,如果您的JSON包含任何数组,解析完成后这些数组仍然是JScript数组。您可以使用点符号从VBScript访问JScript数组的内容。

<%@Language="VBScript" %>
<%
Option Explicit
%>

<script language="JScript" runat="server" src='path/to/json2.js'></script>

<%

Dim myJSON
myJSON = Request.Form("myJSON") // "[ 1, 2, 3 ]"
Set myJSON = JSON.parse(myJSON) // [1,2,3]
Response.Write(myJSON)          // 1,2,3
Response.Write(myJSON.[0])      // 1
Response.Write(myJSON.[1])      // 2
Response.Write(myJSON.[2])      // 3
%>

#2


14  

Not sure about it. Have you checked ASP extreme framework which has JSON support?

不确定。您是否检查过支持JSON的ASP极端框架?

#3


13  

I couldn't get the extreme-evolution or Chris Nielson's suggestion to work. But, the following did work for me:

我不能让极端进化或者克里斯·尼尔森的建议发挥作用。但是,以下方法对我确实有效:

http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP

http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP

Download the following as "json2.min.asp"

下载如下“json2.min.asp”

http://tforster.wik.is/@api/deki/files/2/=json2.min.asp

http://tforster.wik.is/@api . . /文件/ 2 / = json2.min.asp

Add the following line to the top of your ASP file:

将以下一行添加到您的ASP文件的顶部:

<script language="javascript" runat="server" src="json2.min.asp"></script>

You can then use JSON in ASP.

然后可以在ASP中使用JSON。

   Dim car: Set car = JSON.parse("{""brand"":""subaru"",""model"":""outback sport"",""year"":2003," & _
                                 """colour"":""green"",""accessories"":[" & _
                                 "{""foglamps"":true},{""abs"":true},{""heatedSeats"":true}]}")

   Response.Write("brand: " & car.brand & "<br/>")                               
   Response.Write("model: " & car.model & "<br/>")                               
   Response.Write("colour: " & car.colour & "<br/>")                               
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               

   car.accessories.get(0).foglamps = false
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               
   Response.Write("new Json: " & JSON.stringify(car) & "<br/>")

   Set car = Nothing

Note: To parse through an array of items, you need to do the following:

注意:要解析一个项目数组,需要执行以下操作:

   for each iTmp in testing
       if (TypeName(iTmp))<>"JScriptTypeInfo" then 
           Response.Write("Item: " &  iTmp & "<br/>")
       end if
   next

#4


7  

I have recently implemented a VbsJson class, which has a "Decode" method to parse JSON to VBScript and a "Encode" method to generate JSON from VBScript. The code is somewhat long, so I don't paste it here.

我最近实现了一个VbsJson类,它有一个“Decode”方法来解析JSON到VBScript和一个从VBScript生成JSON的“编码”方法。代码有点长,所以我不在这里粘贴。

#5


4  

http://github.com/nagaozen/asp-xtreme-evolution/

http://github.com/nagaozen/asp-xtreme-evolution/

#6


3  

I wrote this answer when I was looking for a light-weight pure VBScript only solution.

我在寻找轻量级纯VBScript唯一解决方案时编写了这个答案。

By putting together a rudimentary JSON to XML converter, we can walk the JSON string and turn it into a Microsoft.XMLDOM document.

通过组装一个基本的JSON到XML转换器,我们可以遍历JSON字符串并将其转换为Microsoft。XMLDOM文档。

From there, we use Microsoft's XML API including XPath queries to pluck out any values we wanted.

从这里开始,我们使用Microsoft的XML API,包括XPath查询来提取我们想要的任何值。

This handles simple JSON, but, I never intended this answer for anything more sophisticated.

它处理的是简单的JSON,但是,我从来没有打算用这个答案来解决更复杂的问题。

For a more robust solution, the best JSON interpreter, is a proper Javascript engine. Therefore, I highly recommend the accepted answer to this question i.e. Any good libraries for parsing JSON in Classic ASP?

对于更健壮的解决方案,最好的JSON解释器是一个合适的Javascript引擎。因此,我强烈推荐这个问题的公认答案,即在经典ASP中解析JSON有什么好的库吗?

Function JSONtoXML(jsonText)
  Dim idx, max, ch, mode, xmldom, xmlelem, xmlchild, name, value

  Set xmldom = CreateObject("Microsoft.XMLDOM")
  xmldom.loadXML "<xml/>"
  Set xmlelem = xmldom.documentElement

  max = Len(jsonText)
  mode = 0
  name = ""
  value = ""
  While idx < max
    idx = idx + 1
    ch = Mid(jsonText, idx, 1)
    Select Case mode
    Case 0 ' Wait for Tag Root
      Select Case ch
      Case "{"
        mode = 1
      End Select
    Case 1 ' Wait for Attribute/Tag Name
      Select Case ch
      Case """"
        name = ""
        mode = 2
      Case "{"
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "["
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "}"
        Set xmlelem = xmlelem.parentNode
      Case "]"
        Set xmlelem = xmlelem.parentNode
      End Select
    Case 2 ' Get Attribute/Tag Name
      Select Case ch
      Case """"
        mode = 3
      Case Else
        name = name + ch
      End Select
    Case 3 ' Wait for colon
      Select Case ch
      Case ":"
        mode = 4
      End Select
    Case 4 ' Wait for Attribute value or Tag contents
      Select Case ch
      Case "["
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case "{"
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case """"
        value = ""
        mode = 5
      Case " "
      Case Chr(9)
      Case Chr(10)
      Case Chr(13)
      Case Else
        value = ch
        mode = 7
      End Select
    Case 5
      Select Case ch
      Case """"
        xmlelem.setAttribute name, value
        mode = 1
      Case "\"
        mode = 6
      Case Else
        value = value + ch
      End Select
    Case 6
      value = value + ch
      mode = 5
    Case 7
      If Instr("}], " & Chr(9) & vbCr & vbLf, ch) = 0 Then
        value = value + ch
      Else
        xmlelem.setAttribute name, value
        mode = 1
        Select Case ch
        Case "}"
          Set xmlelem = xmlelem.parentNode
        Case "]"
          Set xmlelem = xmlelem.parentNode
        End Select
      End If
    End Select
  Wend

  Set JSONtoXML = xmlDom
End Function

The above script, transforms the following JSON:

上面的脚本,转换以下JSON:

{
  "owningSystemUrl": "http://www.arcgis.com",
  "authInfo": {
    "tokenServicesUrl": "https://www.arcgis.com/sharing/rest/generateToken",
    "isTokenBasedSecurity": true
  }
}

into:

成:

<xml owningSystemUrl="http://www.arcgis.com">
    <authInfo
        tokenServicesUrl="https://www.arcgis.com/sharing/rest/generateToken"
        isTokenBasedSecurity="true" >
    </authInfo>
</xml>

We can now use XPath to extract the tokenServicesUrl, for example:

我们现在可以使用XPath提取标记服务url,例如:

dom.SelectSingleNode("xml/authInfo").getAttribute("tokenServicesUrl")
' Returns: "https://www.arcgis.com/sharing/rest/generateToken"

#7


2  

AXE is a great library but is rather heavy if you just need JSON processing functionality.

AXE是一个很棒的库,但是如果您只需要JSON处理功能,它就相当重了。

I did, however, grab the base.asp file and the json.asp class file from the AXE project and successfully used them to implement JSON parsing in my project.

不过,我还是抓住了基地。asp文件和json。来自AXE项目的asp类文件,并成功地使用它们在我的项目中实现JSON解析。

For JSON generation, I found aspjson was simpler to integrate. It also has more powerful json-related features. The axe documentation a little lacking and was more work to integrate into the project, however it does do a fine job of serializing its JSON VB object back to a string.

对于JSON生成,我发现aspjson集成起来更简单。它还有更强大的与json相关的特性。尽管它的文档有点缺乏,但要集成到项目中还需要更多的工作,但是它确实能够很好地将其JSON VB对象序列化为一个字符串。

#8


2  

the solutions here are very good but sometimes overkill. If the JSON is simple and always the same structure you can parse it yourself, it's fast and simple.

这里的解决方案非常好,但有时有些过头了。如果JSON是简单的,并且总是相同的结构,您可以自己解析它,那么它是快速和简单的。

 'read data from client
 records = Request.Form("records")
 'convert the JSON string to an array
 Set oRegExpre = new RegExp
 oRegExpre.Global = true
 oRegExpre.Pattern = "[\[\]\{\}""]+"
 records = replace(records, "},{","||")
 records = oRegExpre.Replace(records, "" )
 aRecords = split(records,"||")
 'iterate the array and do some cleanup
 for each rec in aRecords
   aRecord = split(rec,",")
   id          = split(aRecord(1),":")(1)
   field       = split(aRecord(0),":")(0)
   updateValue = split(aRecord(0),":")(1)
   updateValue = replace(updateValue,chr(10),"\n")
   updateValue = replace(updateValue,chr(13),"\r")
   updateValue = replace(updateValue,"'","''")
  'etc
next

#1


83  

Keep in mind that Classic ASP includes JScript as well as VBScript. Interestingly, you can parse JSON using JScript and use the resulting objects directly in VBScript.

记住,经典的ASP包括JScript和VBScript。有趣的是,您可以使用JScript解析JSON并直接在VBScript中使用结果对象。

Therefore, it is possible to use the canonical https://github.com/douglascrockford/JSON-js/blob/master/json2.js in server-side code with zero modifications.

因此,可以在服务器端代码中使用规范的https://github.com/douglas douglas crock/json -js/blob/master/json2.js,无需修改。

Of course, if your JSON includes any arrays, these will remain JScript arrays when parsing is complete. You can access the contents of the JScript array from VBScript using dot notation.

当然,如果您的JSON包含任何数组,解析完成后这些数组仍然是JScript数组。您可以使用点符号从VBScript访问JScript数组的内容。

<%@Language="VBScript" %>
<%
Option Explicit
%>

<script language="JScript" runat="server" src='path/to/json2.js'></script>

<%

Dim myJSON
myJSON = Request.Form("myJSON") // "[ 1, 2, 3 ]"
Set myJSON = JSON.parse(myJSON) // [1,2,3]
Response.Write(myJSON)          // 1,2,3
Response.Write(myJSON.[0])      // 1
Response.Write(myJSON.[1])      // 2
Response.Write(myJSON.[2])      // 3
%>

#2


14  

Not sure about it. Have you checked ASP extreme framework which has JSON support?

不确定。您是否检查过支持JSON的ASP极端框架?

#3


13  

I couldn't get the extreme-evolution or Chris Nielson's suggestion to work. But, the following did work for me:

我不能让极端进化或者克里斯·尼尔森的建议发挥作用。但是,以下方法对我确实有效:

http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP

http://tforster.wik.is/ASP_Classic_Practices_For_The_21st_Century/JSON4ASP

Download the following as "json2.min.asp"

下载如下“json2.min.asp”

http://tforster.wik.is/@api/deki/files/2/=json2.min.asp

http://tforster.wik.is/@api . . /文件/ 2 / = json2.min.asp

Add the following line to the top of your ASP file:

将以下一行添加到您的ASP文件的顶部:

<script language="javascript" runat="server" src="json2.min.asp"></script>

You can then use JSON in ASP.

然后可以在ASP中使用JSON。

   Dim car: Set car = JSON.parse("{""brand"":""subaru"",""model"":""outback sport"",""year"":2003," & _
                                 """colour"":""green"",""accessories"":[" & _
                                 "{""foglamps"":true},{""abs"":true},{""heatedSeats"":true}]}")

   Response.Write("brand: " & car.brand & "<br/>")                               
   Response.Write("model: " & car.model & "<br/>")                               
   Response.Write("colour: " & car.colour & "<br/>")                               
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               

   car.accessories.get(0).foglamps = false
   Response.Write("has foglamps: " & CStr(car.accessories.get(0).foglamps) & "<br/>")                               
   Response.Write("new Json: " & JSON.stringify(car) & "<br/>")

   Set car = Nothing

Note: To parse through an array of items, you need to do the following:

注意:要解析一个项目数组,需要执行以下操作:

   for each iTmp in testing
       if (TypeName(iTmp))<>"JScriptTypeInfo" then 
           Response.Write("Item: " &  iTmp & "<br/>")
       end if
   next

#4


7  

I have recently implemented a VbsJson class, which has a "Decode" method to parse JSON to VBScript and a "Encode" method to generate JSON from VBScript. The code is somewhat long, so I don't paste it here.

我最近实现了一个VbsJson类,它有一个“Decode”方法来解析JSON到VBScript和一个从VBScript生成JSON的“编码”方法。代码有点长,所以我不在这里粘贴。

#5


4  

http://github.com/nagaozen/asp-xtreme-evolution/

http://github.com/nagaozen/asp-xtreme-evolution/

#6


3  

I wrote this answer when I was looking for a light-weight pure VBScript only solution.

我在寻找轻量级纯VBScript唯一解决方案时编写了这个答案。

By putting together a rudimentary JSON to XML converter, we can walk the JSON string and turn it into a Microsoft.XMLDOM document.

通过组装一个基本的JSON到XML转换器,我们可以遍历JSON字符串并将其转换为Microsoft。XMLDOM文档。

From there, we use Microsoft's XML API including XPath queries to pluck out any values we wanted.

从这里开始,我们使用Microsoft的XML API,包括XPath查询来提取我们想要的任何值。

This handles simple JSON, but, I never intended this answer for anything more sophisticated.

它处理的是简单的JSON,但是,我从来没有打算用这个答案来解决更复杂的问题。

For a more robust solution, the best JSON interpreter, is a proper Javascript engine. Therefore, I highly recommend the accepted answer to this question i.e. Any good libraries for parsing JSON in Classic ASP?

对于更健壮的解决方案,最好的JSON解释器是一个合适的Javascript引擎。因此,我强烈推荐这个问题的公认答案,即在经典ASP中解析JSON有什么好的库吗?

Function JSONtoXML(jsonText)
  Dim idx, max, ch, mode, xmldom, xmlelem, xmlchild, name, value

  Set xmldom = CreateObject("Microsoft.XMLDOM")
  xmldom.loadXML "<xml/>"
  Set xmlelem = xmldom.documentElement

  max = Len(jsonText)
  mode = 0
  name = ""
  value = ""
  While idx < max
    idx = idx + 1
    ch = Mid(jsonText, idx, 1)
    Select Case mode
    Case 0 ' Wait for Tag Root
      Select Case ch
      Case "{"
        mode = 1
      End Select
    Case 1 ' Wait for Attribute/Tag Name
      Select Case ch
      Case """"
        name = ""
        mode = 2
      Case "{"
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "["
        Set xmlchild = xmldom.createElement("tag")
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
      Case "}"
        Set xmlelem = xmlelem.parentNode
      Case "]"
        Set xmlelem = xmlelem.parentNode
      End Select
    Case 2 ' Get Attribute/Tag Name
      Select Case ch
      Case """"
        mode = 3
      Case Else
        name = name + ch
      End Select
    Case 3 ' Wait for colon
      Select Case ch
      Case ":"
        mode = 4
      End Select
    Case 4 ' Wait for Attribute value or Tag contents
      Select Case ch
      Case "["
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case "{"
        Set xmlchild = xmldom.createElement(name)
        xmlelem.appendChild xmlchild
        xmlelem.appendchild xmldom.createTextNode(vbCrLf)
        xmlelem.insertBefore xmldom.createTextNode(vbCrLf), xmlchild
        Set xmlelem = xmlchild
        name = ""
        mode = 1
      Case """"
        value = ""
        mode = 5
      Case " "
      Case Chr(9)
      Case Chr(10)
      Case Chr(13)
      Case Else
        value = ch
        mode = 7
      End Select
    Case 5
      Select Case ch
      Case """"
        xmlelem.setAttribute name, value
        mode = 1
      Case "\"
        mode = 6
      Case Else
        value = value + ch
      End Select
    Case 6
      value = value + ch
      mode = 5
    Case 7
      If Instr("}], " & Chr(9) & vbCr & vbLf, ch) = 0 Then
        value = value + ch
      Else
        xmlelem.setAttribute name, value
        mode = 1
        Select Case ch
        Case "}"
          Set xmlelem = xmlelem.parentNode
        Case "]"
          Set xmlelem = xmlelem.parentNode
        End Select
      End If
    End Select
  Wend

  Set JSONtoXML = xmlDom
End Function

The above script, transforms the following JSON:

上面的脚本,转换以下JSON:

{
  "owningSystemUrl": "http://www.arcgis.com",
  "authInfo": {
    "tokenServicesUrl": "https://www.arcgis.com/sharing/rest/generateToken",
    "isTokenBasedSecurity": true
  }
}

into:

成:

<xml owningSystemUrl="http://www.arcgis.com">
    <authInfo
        tokenServicesUrl="https://www.arcgis.com/sharing/rest/generateToken"
        isTokenBasedSecurity="true" >
    </authInfo>
</xml>

We can now use XPath to extract the tokenServicesUrl, for example:

我们现在可以使用XPath提取标记服务url,例如:

dom.SelectSingleNode("xml/authInfo").getAttribute("tokenServicesUrl")
' Returns: "https://www.arcgis.com/sharing/rest/generateToken"

#7


2  

AXE is a great library but is rather heavy if you just need JSON processing functionality.

AXE是一个很棒的库,但是如果您只需要JSON处理功能,它就相当重了。

I did, however, grab the base.asp file and the json.asp class file from the AXE project and successfully used them to implement JSON parsing in my project.

不过,我还是抓住了基地。asp文件和json。来自AXE项目的asp类文件,并成功地使用它们在我的项目中实现JSON解析。

For JSON generation, I found aspjson was simpler to integrate. It also has more powerful json-related features. The axe documentation a little lacking and was more work to integrate into the project, however it does do a fine job of serializing its JSON VB object back to a string.

对于JSON生成,我发现aspjson集成起来更简单。它还有更强大的与json相关的特性。尽管它的文档有点缺乏,但要集成到项目中还需要更多的工作,但是它确实能够很好地将其JSON VB对象序列化为一个字符串。

#8


2  

the solutions here are very good but sometimes overkill. If the JSON is simple and always the same structure you can parse it yourself, it's fast and simple.

这里的解决方案非常好,但有时有些过头了。如果JSON是简单的,并且总是相同的结构,您可以自己解析它,那么它是快速和简单的。

 'read data from client
 records = Request.Form("records")
 'convert the JSON string to an array
 Set oRegExpre = new RegExp
 oRegExpre.Global = true
 oRegExpre.Pattern = "[\[\]\{\}""]+"
 records = replace(records, "},{","||")
 records = oRegExpre.Replace(records, "" )
 aRecords = split(records,"||")
 'iterate the array and do some cleanup
 for each rec in aRecords
   aRecord = split(rec,",")
   id          = split(aRecord(1),":")(1)
   field       = split(aRecord(0),":")(0)
   updateValue = split(aRecord(0),":")(1)
   updateValue = replace(updateValue,chr(10),"\n")
   updateValue = replace(updateValue,chr(13),"\r")
   updateValue = replace(updateValue,"'","''")
  'etc
next