我有一个Web服务,它返回一个json响应,如下所示:
"database" ; True
"cpu usage" ; 30%
"connection response" ; 1
"memory" ; 48%
要求是创建一个vb脚本,该脚本将读取结果,将其与设置的阈值进行比较并相应地设置标志。 也就是说,如果针对“database”的值为“true”,cpu使用率小于80%,连接响应大于0且内存使用率小于80%,我需要结果说“绿色”。
有人可以帮我解决上述请求。这实际上是用于SCOM监控。
答案 0 :(得分:-1)
你的JSON会更像这样。请注意,我已更改变量名称以删除空格 - 如果这些猜测错误,您将需要相应地修改代码。在JSON中,变量名称和任何非数字值都在引号中。通常你会使用JSON解析器来处理这个问题,但如果它真的很简单,你可以使用一些简单的字符串处理代码来继续。
{
"database": "true",
"cpu_usage": 30,
"connection_response": 1,
"memory": 48
}
调用此函数向其传递从服务获得的JSON字符串。它的工作原理是JSON字符串是一个字符串,如果它是简单的格式,我们可以将它切断以获得可用的值。如果它变成了一个更复杂的消息,那么你需要为VB搜索一个JSON解析器,或者如果接口可以用XML响应,你会发现它在VB中更容易处理。
这是VB6代码(我更容易测试) - 您需要删除所有' as string','作为整数'来自变量的等等声明为VB Script。我已经为来自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