在json对象中循环时出现意外结果
我有一个小应用程序,它对api进行http get调用以检索资源统计数据。 我得到的JSON响应与此类似:在json对象中循环时出现意外结果,json,vb.net,parsing,variables,if-statement,Json,Vb.net,Parsing,Variables,If Statement,我有一个小应用程序,它对api进行http get调用以检索资源统计数据。 我得到的JSON响应与此类似: { "listcapacityresponse" : { "count":6 ,"capacity" : [ {"type":6,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":45660766208,"capacitytotal":10630
{ "listcapacityresponse" : { "count":6 ,"capacity" : [ {"type":6,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":45660766208,"capacitytotal":106308304896,"percentused":"42.95"}, {"type":8,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":5,"capacitytotal":18,"percentused":"27.78"}, {"type":5,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":3,"capacitytotal":12,"percentused":"25"}, {"type":3,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":90202701824,"capacitytotal":1099511627776,"percentused":"8.2"}, {"type":1,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":1500,"capacitytotal":52800,"percentused":"2.84"}, {"type":0,"zoneid":"f26c2094-f2ca-4951-9265-a3f036e7f045","zonename":" CloudPlatform 1","capacityused":1476395008,"capacitytotal":97078222080,"percentused":"1.52"} ] } }
基本上,有6种资源类型,分别是:
0内存使用率
1 CPU使用率
3主存储器
5管理IP
6辅助存储器
8共享网络IP
我像这样解析JSON:
Dim dtoObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of RootObject)(resourceusageresponse)
For Each resource As Capacity In dtoObj.listcapacityresponse.capacity
If resource.type = 0 Then
Dim memoryusedpercent As String = resource.percentused
ElseIf resource.type = 1 Then
Dim cpuusedpercent As String = resource.percentused
ElseIf resource.type = 3 Then
Dim pristorageusedpercent As String = resource.percentused
ElseIf resource.type = 5 Then
Dim mgmtippercent As String = resource.percentused
ElseIf resource.type = 6 Then
Dim secstoragepercent As String = resource.percentused
ElseIf resource.type = 8 Then
Dim guestippercent As String = resource.percentused
End If
Next
我希望if语句中的每个变量在完成解析后都会被填充(我现在只对百分比感兴趣),但是我只得到空变量,没有错误
我错过了什么明显的东西吗
我不顾一切地想让我的项目结束,这是最后一件事站在我的方式
有什么帮助吗!:) 变量将超出If语句的范围。在循环上方调暗它们,您应该会没事。您的变量将超出If语句的范围。在你开始你的循环之前,把那些坏家伙都调暗,然后你就应该有价值了。啊,我明白了,我想改变这些,但是我在顶部有以下内容。。。Public memoryusedpercent As String=“”Public cpuusedpercent As String=“”Public pristorageusedpercent As String=“”Public mgmtippercent As String=“”Public secstoragepercent As String=“”Public guestipercent As String=“”所以我认为这就足够了。我现在就把它们整理好,再试一次。事实上,一旦我整理好,我就会得到我期待的回应!谢谢你,好心的先生!对任何关心的人来说,代码都是一样的,除了if语句,它变成了。。。如果resource.type=0,则memoryusedpercent=resource.percentused ElseIf resource.type=1,然后cpuusedpercent=resource.percentused ElseIf resource.type=3,然后pristorageusedpercent=resource.percentused ElseIf resource.type=5,然后mgmtippercent=resource.percentused ElseIf等。。。。。