使用 VBScript 解码/编码 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12153925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:30:12  来源:igfitidea点击:

Decode/Encode JSON with VBScript

jsonvbscript

提问by Stefan Brendle

I've got a rapid development tool for an ERP-system, which allows only vbscript. I'm trying to create a simple AJAX-Request with VBS. That worked with the "Microsoft.XMLHTTP"-object.

我有一个 ERP 系统的快速开发工具,它只允许使用 vbscript。我正在尝试使用 VBS 创建一个简单的 AJAX 请求。这与“Microsoft.XMLHTTP”对象一起使用。

Next step is to receive data from a webserver using json. But in VBS there seems to be no function like "json_decode" oder other.

下一步是使用 json 从网络服务器接收数据。但是在 VBS 中似乎没有像“json_decode”这样的功能。

Does anyone know a solution? Or is the only option to develop my own json-function?

有谁知道解决方案?或者是开发我自己的 json 函数的唯一选择?

采纳答案by Ekkehard.Horner

As JSON is a hierarchical data format, using Regular expressions and Split(), as Peter proposed, won't get you far.

由于 JSON 是一种分层数据格式,因此使用正则表达式和 Split(),正如 Peter 建议的那样,不会让您走得更远。

If your environment allows CreateObject()you may be able to use a ready made COMponent written in another language (e.g. wrap the standard json2.js in a .WSC or COM enable a .NET DLL). Another option would be to harness another language via the Microsoft Script Control. The con of this approach is that you'll have to deal with the objects/arrays delivered by the other language (some hints are to be found in the topic Peter refered to).

如果您的环境允许,CreateObject()您可以使用用另一种语言编写的现成组件(例如,将标准 json2.js 包装在 .WSC 或 COM 中启用 .NET DLL)。另一种选择是通过 Microsoft Script Control 使用另一种语言。这种方法的缺点是您必须处理由其他语言提供的对象/数组(在 Peter 提到的主题中可以找到一些提示)。

A pure VBScript solution can be found here. I can't read the documentation, but the code compiles and 'works' for simple test cases - YMMV.

可以在此处找到纯 VBScript 解决方案。我无法阅读文档,但是代码可以编译并且可以用于简单的测试用例 - YMMV。

回答by Logan

How about doing this with ASPJSON?
Available from http://www.aspjson.com/

用 ASPJSON 做这个怎么样?
可从http://www.aspjson.com/ 获得

I'm about to use this as a solution for a very old site to send an ajax call (using Jquery) with the encoded data to a MongoDB, for testing.

我即将使用它作为一个非常古老的站点的解决方案,将带有编码数据的 ajax 调用(使用 Jquery)发送到 MongoDB,以进行测试。

回答by Stephen Quan

I had a similar problem so I wrote a JSONtoXML function in VBScript for one of my projects. No warranties on this script (it's provided as-is and has known limitations such as not handling all types of escape sequences):

我遇到了类似的问题,所以我在 VBScript 中为我的一个项目编写了一个 JSONtoXML 函数。对这个脚本没有任何保证(它按原样提供并且具有已知的限制,例如不处理所有类型的转义序列):

Const stateRoot = 0
Const stateNameQuoted = 1
Const stateNameFinished = 2
Const stateValue = 3
Const stateValueQuoted = 4
Const stateValueQuotedEscaped = 5
Const stateValueUnquoted = 6
Const stateValueUnquotedEscaped = 7

Function JSONToXML(json)
  Dim dom, xmlElem, i, ch, state, name, value
  Set dom = CreateObject("Microsoft.XMLDOM")
  state = stateRoot
  For i = 1 to Len(json)
    ch = Mid(json, i, 1)
    Select Case state
    Case stateRoot
      Select Case ch
      Case "["
        If dom.documentElement is Nothing Then
          Set xmlElem = dom.CreateElement("ARRAY")
          Set dom.documentElement = xmlElem
        Else
          Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
        End If
      Case "{"
        If dom.documentElement is Nothing Then
          Set xmlElem = dom.CreateElement("OBJECT")
          Set dom.documentElement = xmlElem
        Else
          Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
        End If
      Case """"
        state = stateNameQuoted 
        name = ""
      Case "}"
        Set xmlElem = xmlElem.parentNode
      Case "]"
        Set xmlElem = xmlElem.parentNode
      End Select
    Case stateNameQuoted 
      Select Case ch
      Case """"
        state = stateNameFinished
      Case Else
        name = name + ch
      End Select
    Case stateNameFinished
      Select Case ch
      Case ":"
        value = ""
        State = stateValue
      End Select
    Case stateValue
      Select Case ch
      Case """"
        State = stateValueQuoted
      Case "{"
        Set xmlElem = XMLCreateChild(xmlElem, "OBJECT")
        State = stateRoot
      Case "["
        Set xmlElem = XMLCreateChild(xmlElem, "ARRAY")
        State = stateRoot
      Case " "
      Case Chr(9)
      Case vbCr
      Case vbLF
      Case Else
        value = ch
        State = stateValueUnquoted
      End Select
    Case stateValueQuoted
      Select Case ch
      Case """"
        xmlElem.setAttribute name, value
        state = stateRoot
      Case "\"
        state = stateValueQuotedEscaped
      Case Else
        value = value + ch
      End Select
    Case stateValueQuotedEscaped ' @@TODO: Handle escape sequences
      value = value + ch
      state = stateValueQuoted
    Case stateValueUnquoted
      Select Case ch
      Case "}"
        xmlElem.setAttribute name, value
        Set xmlElem = xmlElem.parentNode
        state = stateRoot
      Case "]"
        xmlElem.setAttribute name, value
        Set xmlElem = xmlElem.parentNode
        state = stateRoot
      Case ","
        xmlElem.setAttribute name, value
        state = stateRoot
      Case "\"
         state = stateValueUnquotedEscaped
      Case Else
        value = value + ch
      End Select
    Case stateValueUnquotedEscaped ' @@TODO: Handle escape sequences
      value = value + ch
      state = stateValueUnquoted
    End Select
  Next
  Set JSONToXML = dom
End Function

Function XMLCreateChild(xmlParent, tagName)
  Dim xmlChild
  If xmlParent is Nothing Then
    Set XMLCreateChild = Nothing
    Exit Function
  End If
  If xmlParent.ownerDocument is Nothing Then
    Set XMLCreateChild = Nothing
    Exit Function
  End If
  Set xmlChild = xmlParent.ownerDocument.createElement(tagName)
  xmlParent.appendChild xmlChild
  Set XMLCreateChild = xmlChild
End Function

回答by Keith

Check out https://github.com/rcdmk/aspJSON

查看https://github.com/rcdmk/aspJSON

Not sure if this has any relation to www.ASPJSON.com (now defunct) mentioned in Logan's answer.

不确定这与 Logan 的回答中提到的 www.ASPJSON.com(现已不存在)是否有任何关系。

回答by peter

You should better roll out your own based on a query here on json and asp. Like eg this one Any good libraries for parsing JSON in Classic ASP?Most of the time json2 library is used but this is based on jscript so no option for you. Also most of the time this kind of JSON has a fixed structure so it should not be so difficult to parse with a Regularexpression like i demonstrated in a few answer like the one above. You could publish some of your JSON so that we can test it with some routines.

您应该更好地根据 json 和 asp 上的查询推出自己的产品。像这样一个 在经典 ASP 中解析 JSON 的好库吗?大多数情况下使用 json2 库,但这是基于 jscript 的,因此您没有选择。此外,大多数情况下,这种 JSON 具有固定结构,因此使用正则表达式解析应该不会那么困难,就像我在上面的几个答案中演示的那样。您可以发布一些 JSON,以便我们可以使用一些例程对其进行测试。