2013-04-04 74 views
0

我在客戶端創建一個JSON對象,然後將它傳遞到asp:HiddenField如何在Visual Basic中循環客戶端對象數組?

這裏是Object

"[{"value":"0","column":"lngTask"},{"value":"End Checklist","column":"strTask"}, 
    {"value":"0","column":"lngChecklistRevision"}, 
    {"value":"","column":"lngManagedTask"}......]" 

隨後的一部分,我想用它在我後面的代碼我使用​​3210

所以我用JavaScriptSerializer()像這樣:

Dim jss As New JavaScriptSerializer() 
Dim lstReport As List(Of Object) = jss.Deserialize(Of List(Of Object)) 
    (hfObjSqlGridRow.Value) 

這裏是我的lstReport看起來像:

enter image description here

我的問題是我怎樣才能通過這個對象

我已經嘗試的東西像循環:

lsReport(0)(0) 
lsReport(0).(0).value 
lsReport(0).value 

沒有什麼工作,我得到此錯誤= 給定的密鑰不在字典中。

回答

0

此代碼是C#,但應該很容易轉換爲VB.NET。基本前提是使用動態JSON序列化程序,該程序允許在運行時訪問屬性,如JavaScript中的JSON對象。您必須使用.NET 4.0進行動態對象支持。

Deserialize JSON into C# dynamic object?

+0

同樣應嚴格關閉VB設置選項適用。 – 2013-04-04 19:38:47

0

難道你們就不能只是使用For Each循環?

For Each item In lsReport 
    ' Do whatever you need with item.value 
Next 
0

試試這個:

dim JSObject as String = 
[ 
    {"00ID": "PTDA_000ParentCat_000Cat_000SubCat_000_ParentCatTxt", "00Val": "Page"}, 
    {"00ID": "PTDA_000ParentCat_000Cat_000SubCat_001_CatTxt", "00Val": "Inherite Parent"}, 
    {"00ID": "PTDA_000ParentCat_000Cat_000SubCat_002_SubCatTxt", "00Val": "Inherite Parent"}, 
    {"00ID": "PTDA_000ParentCat_000Cat_000SubCat_010_UCI", "00UCItype" : "UCItf", "00Val": "false", "01Txt": "Include related containers", "02Tip": "include related containers"} 
] 

      Dim serializer As JavaScriptSerializer = New JavaScriptSerializer() 
      Dim obj As Object = serializer.Deserialize(Of Object)(JSObject) 

    s = GetProp(obj, "PTDA_000ParentCat_000Cat_000SubCat_000_ParentCatTxt.00Val") 
      SetProp(obj, "PTDA_000ParentCat_000Cat_000SubCat_000_ParentCatTxt.00Val", "NEwVal") 
      s = GetProp(obj, "PTDA_000ParentCat_000Cat_000SubCat_000_ParentCatTxt.00Val") 
      s = "" 

Public Function GetProp(objf As Object, propPath As String) As String 
    Dim propVal As String = "" 
    Dim propPathAr As Array = Split(propPath, ".") 
    'http://stackoverflow.com/questions/8118019/vb-net-json-deserialize 
    For Each item In objf 
     If item("00ID") = propPathAr(0) Then 
      propVal = item(propPathAr(1)) 
      Exit For 
     End If 
    Next 
    Return propVal 
End Function 
Public Sub SetProp(ByRef objf As Object, propPath As String, val As String) 
    Dim propPathAr As Array = Split(propPath, ".") 
    For Each item In objf 
     If item("00ID") = propPathAr(0) Then 
      item(propPathAr(1)) = val 
      Exit Sub 
     End If 
    Next 
End Sub 
+0

請添加一些解釋並編輯代碼,這是完全搞砸了。 – gofr1 2016-05-07 10:56:08