如何在Visual Basic中循环客户端对象数组?

时间:2013-04-04 17:46:24

标签: c# asp.net vb.net json object

我在客户端创建了一个JSON对象,然后将其传递给asp:HiddenField

以下是Object

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

然后我想在我使用Visual Basic

后面的代码中使用它

所以我使用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

没有任何作用我得到这个错误= 字典中没有给定的密钥。

3 个答案:

答案 0 :(得分:0)

此代码在C#中,但应该很容易转换为VB.NET。基本前提是使用动态JSON序列化程序,允许在运行时访问属性,就像JavaScript中的JSON对象一样。您必须使用.NET 4.0进行动态对象支持。

Deserialize JSON into C# dynamic object?

答案 1 :(得分:0)

你能不能只使用For Each循环?

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

答案 2 :(得分: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