2014-01-30 69 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語句的範圍。把它們放在循環的上方,你應該沒問題。