0

我在客户端创建了一个 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样子:

在此处输入图像描述

我的问题是我怎样才能遍历这个对象

我试过这样的事情:

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

没有任何效果我得到这个错误=字典中不存在给定的键。

4

3 回答 3

0

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

将 JSON 反序列化为 C# 动态对象?

于 2013-04-04T18:05:20.857 回答
0

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

For Each item In lsReport
    ' Do whatever you need with item.value
Next
于 2013-04-04T18:05:34.413 回答
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
于 2016-05-07T10:48:36.087 回答