2014-01-30 72 views
0

我有一个小应用程序,它使http调用api来检索资源状态。 我得到类似于此的JSON响应:当通过json对象循环时意外的结果

{ "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共享的网络IPS

我解析的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语句中的每个变量在完成解析后都会被填充(我现在只对真正的百分比感兴趣),但是我只是得到空变量而没有错误。

我是否缺少明显的东西?

我迫不及待想把我的项目的这一点关闭,这是我最后一件事!

任何帮助appriciated! :)

+1

您的变量将超出If语句的范围。在你开始你的循环之前,黯淡那些坏男孩,然后你应该有价值观。 – N0Alias

+0

啊我明白了,我的意思是改变这些,但是我有在顶部以下... 公共memoryusedpercent的String =“” 公共cpuusedpercent的String =“” 公共pristorageusedpercent的String =“” 公共mgmtippercent As String =“” Public secstoragepercent As String =“” Public guestiperscent As String =“” 所以我会认为这已经足够了。 我会整理他们现在再试一次 – John

+0

ahhh的确,一旦我收拾起来,我有我期待的回应! 谢谢你亲切的先生! – John

回答

0

变量将超出If语句的范围。把它们放在循环的上方,你应该没问题。