2012-08-28 139 views
12

我有一個ERP系統的快速開發工具,它只允許vbscript。我試圖用VBS創建一個簡單的AJAX請求。這與「Microsoft.XMLHTTP」 - 對象一起工作。用VBScript解碼/編碼JSON

下一步是使用json從網絡服務器接收數據。但在VBS中似乎沒有像「json_decode」或其他功能。

有誰知道解決方案嗎?或者是開發我自己的json函數的唯一選擇?

+0

使用這個,似乎我的情況下工作: [demon.tw(http://demon.tw/my-work/vbs-json.html#code) –

回答

8

由於JSON是一種分層數據格式,正如Peter提出的那樣,使用正則表達式和Split()不會讓你走得太遠。

如果您的環境允許CreateObject()您可能可以使用以其他語言編寫的現成的組件(例如,在.WSC中包裝標準json2.js或在.NET中啓用.NET DLL)。另一種選擇是通過Microsoft Script Control利用另一種語言。這種方法的意義在於,你必須處理由另一種語言交付的對象/數組(有些提示可以在Peter所說的主題中找到)。

可以找到一個純VBScript解決方案here。我無法閱讀文檔,但代碼編譯並適用於簡單測試用例 - YMMV。

+0

從demon.tw解決方案的偉大工程,但表現非常緩慢。我有一個10-15個簡單值的簡單「數組」。一個數組包含一個產品的信息。我有3500個產品,所以我必須運行JSON編碼3500倍。 對於這項任務它需要2-3分鐘。如果沒有JSON編碼,它運行得很快,但我需要這個JSON格式。有沒有可能加速它? 30秒是可以接受的。 –

+0

我已經通過手動構建JSON語法解決了這個問題。我剛剛加入了字符串並用JSON-Syntax包裝了它們,並且逃脫了一些特殊的字符。所以表現很棒 - 並且適用於我的情況。 –

+0

_e.g。將標準json2.js包裝在.WSC或COM中啓用a。NET DLL_你可以請我指向網站或任何文檔如何做到這一點? – Gurman

0

你應該更好地推出自己的基於json和asp的查詢。像這樣一個Any good libraries for parsing JSON in Classic ASP? 大部分時間json2庫被使用,但這是基於jscript所以沒有選擇。 也大多數時候這種JSON有一個固定的結構,所以它不應該像我在上面的幾個答案中顯示的Regularexpression解析那麼困難。 您可以發佈一些JSON,以便我們可以使用一些例程對其進行測試。

3

我有一個類似的問題,所以我在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 
+0

感謝偉大的功能斯蒂芬。請回顧以下適合您的想法的項目。 https://github.com/pravynandas/JSONToXML – PravyNandas

8

如何與ASPJSON這樣做呢?
可從http://www.aspjson.com/

我就要用這個作爲一個解決方案,一個很老的網站與編碼數據的發送的MongoDB Ajax調用(使用jQuery),進行測試。

+1

這是最好的解決方案,它的工作原理和性能都很好。 –