JSON이 더 유사합니다. 공백을 제거하기 위해 변수 이름을 변경 했으므로 변수가 잘못 되었다면 코드를 수정해야합니다. JSON에서 변수 이름과 숫자가 아닌 값은 따옴표 안에 있습니다. 일반적으로 이것을 처리하기 위해 JSON 구문 분석기를 사용하지만 실제로이 간단한 경우 진행할 간단한 문자열 처리 코드를 사용할 수 있습니다.
서비스에서 가져온 JSON 문자열을 전달하는이 함수를 호출하십시오. 그것은 JSON 문자열이 문자열이라는 것을 기초로 작동하며 단순한 형식 인 경우 사용할 수있는 값을 얻기 위해 잘라낼 수 있습니다. 보다 복잡한 메시지가되면 VB 용 JSON 파서를 검색해야합니다. 그렇지 않으면 인터페이스가 XML로 응답 할 수 있으면 VB에서 처리하기가 훨씬 쉬워집니다.
이것은 VB6 코드입니다. 테스트하기 쉽습니다. VB 스크립트를 선언 한 변수에서 'as string', 'as integer'등을 모두 제거해야합니다. 내 함수로 테스트하지 않았지만 here에서 나온 VBScript 용 val() 함수를 포함 시켰습니다. JSON은 문자열 형식이므로 val()이 필요하며 숫자 값을 문자열과 비교하려고하면 예기치 않은 결과가 발생합니다.
'
' 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 - 이것은 내가 webservice에서받을 예정 제안 응답이었고 따라서 나는 내가 webservice에서 나오는 것이라고 생각 무언가를 무언가 적었습니다. 웹 서비스 자체는 아직 구축되지 않았습니다. 그러나 나는 그것을 모니터하는 방법을 찾으려고 노력했다. –
죄송합니다. * * "json 응답을 반환하는 웹 서비스가 있습니다."* * 이것이 순전히 개념적이라면 질문은 명확한 문제가 있어야하며 도움을 줄 사람을 위해 [mcve]를 통해 쉽게 다시 작성할 수 있어야하므로 질문 할 수있는 적절한 곳이 아닙니다. 게시하기 전에 [ask]를 검토하십시오. – Lankymart