0

I created a JSON object in the client side And then Pass it into a asp:HiddenField

Here is a part Of that Object

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

Then I wanted to Use it in my code behind I'm Using Visual Basic

So i used JavaScriptSerializer() Like this :

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

Here is how my lstReport Looks like :

enter image description here

My Question Is how can i loop through this object

i have tried things like :

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

Nothing works i get this Error = The given key was not present in the dictionary.

Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124

3 Answers3

0

This code is in C#, but should be easy enough to convert to VB.NET. The basic premise is to use a dynamic JSON serializer that allows accessing properties at run-time like a JSON object in JavaScript. You must be using .NET 4.0 for dynamic object support.

Deserialize JSON into C# dynamic object?

Community
  • 1
  • 1
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
0

Can you not just use the For Each loop?

For Each item In lsReport
    ' Do whatever you need with item.value
Next
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
0

Try this:

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
Pang
  • 9,564
  • 146
  • 81
  • 122