19

vbscript のみを許可する ERP システム用の迅速な開発ツールを持っています。VBS で単純な AJAX-Request を作成しようとしています。これは、「Microsoft.XMLHTTP」オブジェクトで機能しました。

次のステップは、json を使用して Web サーバーからデータを受信することです。しかし、VBSには「json_decode」などの機能がないようです。

誰かが解決策を知っていますか? または、独自のjson関数を開発する唯一のオプションですか?

4

5 に答える 5

13

JSON は階層データ形式であるため、Peter が提案したように、正規表現と Split() を使用しても、うまくいきません。

環境が許せCreateObject()ば、別の言語で記述された既製のコンポーネントを使用できる場合があります (たとえば、標準の json2.js を .WSC でラップするか、COM で .NET DLL を有効にします)。もう 1 つのオプションは、Microsoft Script Control を介して別の言語を利用することです。このアプローチの短所は、他の言語によって提供されるオブジェクト/配列を処理する必要があることです (Peter が参照したトピックにいくつかのヒントがあります)。

純粋な VBScript ソリューションは、ここにあります。ドキュメントを読むことはできませんが、コードはコンパイルされ、単純なテスト ケース (YMMV) で「機能」します。

于 2012-08-28T09:15:02.030 に答える
10

ASPJSONでこれを行うのはどうですか? http://www.aspjson.com/
から入手可能

テストのために、エンコードされたデータを含む ajax 呼び出し (Jquery を使用) を MongoDB に送信するための非常に古いサイトのソリューションとして、これを使用しようとしています。

于 2013-09-03T00:30:16.303 に答える
4

私も同様の問題を抱えていたので、自分のプロジェクトの 1 つに 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
于 2012-08-29T06:10:36.163 に答える
0

ここでは、json と asp のクエリに基づいて、独自のものを展開することをお勧めします。たとえば、これのよう に、クラシック ASP で JSON を解析するための適切なライブラリはありますか? ほとんどの場合、json2 ライブラリが使用されますが、これは jscript に基づいているため、オプションはありません。また、ほとんどの場合、この種の JSON は構造が固定されているため、上記のようないくつかの回答で示したように、Regularexpression で解析することはそれほど難しくありません。JSON の一部を公開して、いくつかのルーチンでテストできるようにすることができます。

于 2012-08-28T08:22:24.557 に答える