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

Last Reply 6 months ago By pandeyism

Posted 6 months 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("http://192.168.1.239/Generic_Handler_JSON/HandlerVBEvents.ashx")
        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())

        Next

 

You are viewing reply posted by: pandeyism 6 months ago.
Posted 6 months ago Modified on 6 months ago

Hey makumbi,

Please refer below sample.

Namespaces

C#

using System.IO;

VB.Net

Imports System.IO

Code

C#

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>");
}

VB.Net

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))
    Next
    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

Output

CustomerId is 05-00001

comments is Jana Sports Day Please Come Early