Read JSON string Array and assign value to string variable in C# and VB.Net

Last Reply one year ago By pandeyism

Posted one year ago

how can i assign json array to string please help

"[{""CustomerId"":""05-00001"",""Comments"":""Jana Sports Day Please Come Early"",""Dates"":""\/Date(1543525200000)\/"",""Status"":null,""Class"":""P1"",""Stream"":null,""Term"":null,""Years"":null}]"


        Dim json As String = New System.Net.WebClient().DownloadString("")
        Dim count As String = json.Split(":"c)(1).Replace("}", "")

        Dim array As Array = json.ToArray
        For Each item As Object In array(json)
            Dim name As String = If(item("name") Is Nothing, "", item("name").ToString())
            Dim comments As String = If(item("comments") Is Nothing, "", item("comments").ToString())



Posted one year ago Modified on one year ago

Hey makumbi,

Please refer below sample.



using System.IO;


Imports System.IO



protected void Page_Load(object sender, EventArgs e)
    string json = File.ReadAllText(Server.MapPath("~/JSon.txt"));
    string[] values = json.Replace("\"[{", "").Replace("}]\"", "").Replace("\"", "").Split(',');
    Dictionary<string, string> dic = new Dictionary<string, string>();
    foreach (var item in values)
        dic.Add(item.Split(':')[0], item.Split(':')[1]);
    string customerId = dic["CustomerId"];
    string comments = dic["Comments"];
    Response.Write("CustomerId is <b>" + customerId + "</b><br/>comments is <b>" + comments + "</b>");


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim json As String = File.ReadAllText(Server.MapPath("~/JSon.txt"))
    Dim values As String() = json.Replace("""[{", "").Replace("}]""", "").Replace("""", "").Split(","c)
    Dim dic As Dictionary(Of String, String) = New Dictionary(Of String, String)()
    For Each item In values
        dic.Add(item.Split(":"c)(0), item.Split(":"c)(1))
    Dim customerId As String = dic("CustomerId")
    Dim comments As String = dic("Comments")
    Response.Write("CustomerId is <b>" & customerId & "</b><br/>comments is <b>" & comments & "</b>")
End Sub


CustomerId is 05-00001

comments is Jana Sports Day Please Come Early