Search code examples
jsonvb.netjson.net

Loop the contents of a JSON array object


I am new to Vb.net. Having a JSON string as given below, I tries to deserialize the JSON into an Object using JsonConvert.DeserializeObject().
I am trying to loop values inside the Content object in the given JSON to fetch its values.
I tried using a for loop, but I'm not able to get the exact values.

  Dim result As String = {
  "status": "0001",
  "Result": {
    "IsError": "0",
    "Data": {
      "Type": "a",
      "Header": [
        "v1",
        "v2",
        "v3",
        "v4",
        "v5"
      ],
      "Content": [
        [
          "001",
          "Raj",
          "1",
          "N",
          ""
        ],
        [
          "002",
          "Vignesh",
          "1",
          "N",
          ""
        ],
        [
          "778",
          "Ramesh",
          "1",
          "N",
          ""
        ],
        [
          "792",
          "Suresh",
          "1",
          "N",
          ""
        ],
        [
          "703",
          "Karthick",
          "1",
          "N",
          ""
        ],
        [
          "1247",
          "Ram",
          "1",
          "N",
          ""
        ]
      ]
    }
  }
}

Dim jsonResult2 = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(result)

Solution

  • If you just want to deserialize the Content array, you can parse the JSON with JToken.Parse(), then deserialize to a List(Of String()) that section only.

    Dim jsonResult = JToken.Parse(result)
    Dim content = JsonConvert.DeserializeObject(Of List(Of String()))(
        jsonResult("Result")("Data")("Content").ToString()
    )
    

    Or, you could use a class Model to deserialize the whole JSON, then access each object as a standard .Net Property value.

    Public Class StatusResultsRoot
        <JsonProperty("status")>
        Public Property Status As String
        Public Property Result As Result
    End Class
    
    Public Partial Class Result
        Public Property IsError As String
        Public Property Data As Data
    End Class
    
    Public Partial Class Data
        <JsonProperty("Type")>
        Public Property DataType As String
        Public Property Header As List(Of String)
        Public Property Content As List(Of String())
    End Class
    
    '[...]
    
    Dim statusResult = JsonConvert.DeserializeObject(Of StatusResultsRoot)(result)
    

    The Content List is then

    Dim content As List(Of String()) = statusResult.Result.Data.Content
    
    ' Loop the List of String(), print the combined array of strings
    For Each stringArray As String() In content
        Console.WriteLine(String.Join(", ", stringArray))
    Next
    

    In case you actually want to embed a JSON as a string, you can use an XML element literal, defining an XElement by enclosing the JSON with <node> ... </node> markers.
    You can then paste in the JSON as is (no need to double the double-quotes).
    The JSON is then the string representation of the first node of the XElement: i.e., [XElement].FirstNode.ToString(). For example:

    Dim xResult = <json> {
        "status":  "0001",
        "Result": {
            "IsError": "0",
            "Data": {
                ' [... other content ...]
            }
        }
    } </json>
    
    Dim json As String = xResult.FirstNode.ToString()
    

    You cannot do this in C#.