你的JSON會更像這樣。請注意,我已更改變量名稱以刪除空格 - 如果這些猜測錯誤,則需要相應地修改代碼。在JSON中,變量名稱和任何非數字值都在引號中。通常你會使用JSON解析器來處理這個問題,但如果真的這麼簡單,你可以使用一些簡單的字符串處理代碼來繼續。
{
"database": "true",
"cpu_usage": 30,
"connection_response": 1,
"memory": 48
}
調用此函數將它從服務中獲取的JSON字符串傳遞給它。它的工作原理是JSON字符串是一個字符串,如果它是簡單的格式,我們可以將它切斷以獲取可用值。如果它變成一個更復雜的消息,那麼你將需要搜索一個用於VB的JSON解析器,或者如果接口可以用XML響應,你會發現在VB中處理起來要容易得多。
這是VB6代碼(我更容易測試) - 您將需要從變量聲明的VB腳本中刪除所有'as string','as integer'等。我已經包含了一個來自here的VBScript的val()函數,雖然沒有使用我的函數進行測試。您需要val(),因爲JSON是字符串格式的,如果您嘗試將數值與字符串進行比較,您會得到意想不到的結果。
'
' Function to return RED or GREEN depending on values in simple JSON
'
Function checkStatus(sJSON As String) As String
Dim aVals() As String, aParams() As String, i As Integer, sName As String, sVal As String
Dim bDatabase As Boolean, bCPU As Boolean, bConnection As Boolean, bMemory As Boolean
aVals = Split(sJSON, ",")
For i = 0 To UBound(aVals)
aVals(i) = Trim(aVals(i)) ' remove any leading & trailing spaces
aVals(i) = Replace(aVals(i), "{", "") ' remove braces open
aVals(i) = Replace(aVals(i), "}", "") ' remove braces close
aVals(i) = Replace(aVals(i), """", "") ' remove quotes > "database: true"
Debug.Print "vals[" & i & "]=" & aVals(i)
If Len(aVals(i)) > 0 Then ' should catch any dodgy JSON formatting but may need refinement
aParams = Split(aVals(i), ":") ' split the line e.g. "database: true" > "database" and " true"
If UBound(aParams) > 0 Then
sName = LCase(Trim(aParams(0))) ' now we have sName = "database"
sVal = LCase(Trim(aParams(1))) ' and sVal = "true"
Select Case sName
Case "database"
bDatabase = False
If sVal = "true" Then
bDatabase = True
End If
Case "cpu_usage"
bCPU = False
If Val(sVal) > 80 Then
bCPU = True
End If
Case "connection_response"
bConnection = False
If Val(sVal) > 0 Then
bConnection = True
End If
Case "memory"
bMemory = False
If Val(sVal) < 80 Then
bMemory = True
End If
End Select
End If
End If
Next i
checkStatus = "RED" ' default return value to indicate an issue
' compare the flags to decide if all is well.
If bDatabase And bCPU Then 'And bConnection And bMemory Then
checkStatus = "GREEN"
End If
End Function
Function Val(myString)
' Val Function for VBScript (aka ParseInt Function in VBScript).
' By Denis St-Pierre.
' Natively VBScript has no function to extract numbers from a string.
' Based shamelessly on MS' Helpfile example on RegExp object.
' CAVEAT: Returns only the *last* match found
' (or, with objRE.Global = False, only the *first* match)
Dim colMatches, objMatch, objRE, strPattern
' Default if no numbers are found
Val = 0
strPattern = "[-+0-9]+" ' Numbers positive and negative; use
' "ˆ[-+0-9]+" to emulate Rexx' Value()
' function, which returns 0 unless the
' string starts with a number or sign.
Set objRE = New RegExp ' Create regular expression object.
objRE.Pattern = strPattern ' Set pattern.
objRE.IgnoreCase = True ' Set case insensitivity.
objRE.Global = True ' Set global applicability:
' True => return last match only,
' False => return first match only.
Set colMatches = objRE.Execute(myString) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
Val = objMatch.Value
Next
Set objRE= Nothing
End Function
JSON格式錯誤,應該是屬性名稱:屬性值。分號*(';')*是JavaScript中的命令終止字符,您應該使用冒號*(':')*代替。 – Lankymart
感謝Lankymart - 這是一個建議的迴應,我將從Web服務接收,因此我寫了一些我認爲會從Web服務中出來的東西。該webservice本身尚未建成。但我試圖找出一種方法來監控它。 –
對不起,我沒有從 - *「我有一個web服務返回一個json響應」*。如果目前這純粹是概念性的,那麼這不是一個正確的問題,因爲問題應該有一個明確的問題,並且可以通過[mcve]輕鬆地爲任何想要幫助的人重新創建。發佈前請檢查[問]。 – Lankymart