Read and Parse JSON string in ASP.Net using C# and VB.Net

ghjg6396
 
on Dec 19, 2021 10:49 PM
436 Views

Hi,

How can I parse this Json result in VB.Net or C#.

{
  "data": {
    "id": "effC879",
    "keyType": "locus",
    "returnStatus": "verified",
    "clientId": "C879",
    "contactId": "C879",
    "taxProducts": [
      {
        "entity": "123",
        "jurisdiction": "US",
        "taxYear": 2020,
        "returnData": [
          {
            "key": {
              "unit": "1",
              "field": "101",
              "screen": "44"
            },
            "value": "X",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1100",
              "screen": "404"
            },
            "value": "2",
            "type": "field"
          },          
          {
            "key": {
              "unit": "1",
              "field": "1041",
              "screen": "55"
            },
            "value": "100000",
            "type": "statement",
            "rows": [
              {
                "id": "0",
                "sequence": "2",
                "columns": [
                  {
                    "id": "0",
                    "value": "1"
                  },
                  {
                    "id": "1",
                    "value": "Salaries and wages"
                  },
                  {
                    "id": "2",
                    "value": "100000"
                  }
                ]
              }
            ]
          },
          {
            "key": {
              "unit": "1",
              "field": "1042",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1044",
              "screen": "55"
            },
            "value": "24800",
            "type": "statement",
            "rows": [
              {
                "id": "0",
                "sequence": "1",
                "columns": [
                  {
                    "id": "0",
                    "value": "8"
                  },
                  {
                    "id": "1",
                    "value": "Standard deduction"
                  },
                  {
                    "id": "2",
                    "value": "24800"
                  }
                ]
              }
            ]
          },
          {
            "key": {
              "unit": "1",
              "field": "1048",
              "screen": "55"
            },
            "value": "8632",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1279",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          }
        ]
      }
    ]
  }
}

 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Dec 20, 2021 12:20 AM

Hi ghjg6396,

 Please refer below sample.

JSON

{
  "data": {
    "id": "effC879",
    "keyType": "locus",
    "returnStatus": "verified",
    "clientId": "C879",
    "contactId": "C879",
    "taxProducts": [
      {
        "entity": "123",
        "jurisdiction": "US",
        "taxYear": 2020,
        "returnData": [
          {
            "key": {
              "unit": "1",
              "field": "101",
              "screen": "44"
            },
            "value": "X",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1100",
              "screen": "404"
            },
            "value": "2",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "283",
              "screen": "17"
            },
            "value": "CA",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "284",
              "screen": "17"
            },
            "value": "7.25",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "414",
              "screen": "3"
            },
            "value": "ABC LLC",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "419",
              "screen": "3"
            },
            "value": "12-3456789",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "429",
              "screen": "3"
            },
            "value": "1000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "430",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "431",
              "screen": "3"
            },
            "value": "6200",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "432",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "434",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "435",
              "screen": "3"
            },
            "value": "1450",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "440",
              "screen": "3"
            },
            "value": "2000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "441",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "444",
              "screen": "3"
            },
            "value": "3000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "445",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "447",
              "screen": "3"
            },
            "value": "CA",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "449",
              "screen": "3"
            },
            "value": "T",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "450",
              "screen": "3"
            },
            "value": "2020",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "451",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "454",
              "screen": "3"
            },
            "value": "3030",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "455",
              "screen": "3"
            },
            "value": "100000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "457",
              "screen": "3"
            },
            "value": "NY",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "590",
              "screen": "3"
            },
            "value": "ABC LLC",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "260",
              "screen": "65"
            },
            "value": "P.O. Box 802501",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "261",
              "screen": "65"
            },
            "value": "Cincinnati, OH 45280-2501",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "262",
              "screen": "65"
            },
            "value": "P.O. Box 802502",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "263",
              "screen": "65"
            },
            "value": "Ogden, UT 84201 (Cincinnati)",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "276",
              "screen": "65"
            },
            "value": "Cincinnati, OH 45280-2502",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "290",
              "screen": "65"
            },
            "value": "5",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "295",
              "screen": "65"
            },
            "value": "5",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "296",
              "screen": "65"
            },
            "value": "5",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "297",
              "screen": "65"
            },
            "value": "5",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "15",
              "screen": "1"
            },
            "value": "100",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "78",
              "screen": "1"
            },
            "value": "Consultant",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "79",
              "screen": "1"
            },
            "value": "House Wife",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "95",
              "screen": "1"
            },
            "value": "2",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "261",
              "screen": "1"
            },
            "value": "Jack & Jill",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "262",
              "screen": "1"
            },
            "value": " ",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "401",
              "screen": "1"
            },
            "value": "Jack",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "403",
              "screen": "1"
            },
            "value": "Anderson",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "404",
              "screen": "1"
            },
            "value": "111-11-1111",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "411",
              "screen": "1"
            },
            "value": "Jill",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "413",
              "screen": "1"
            },
            "value": "Anderson",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "414",
              "screen": "1"
            },
            "value": "222-22-2222",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "421",
              "screen": "1"
            },
            "value": "17890 Sky Park Circle",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "422",
              "screen": "1"
            },
            "value": "Irvine",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "423",
              "screen": "1"
            },
            "value": "CA",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "424",
              "screen": "1"
            },
            "value": "92614",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1041",
              "screen": "55"
            },
            "value": "100000",
            "type": "statement",
            "rows": [
              {
                "id": "0",
                "sequence": "2",
                "columns": [
                  {
                    "id": "0",
                    "value": "1"
                  },
                  {
                    "id": "1",
                    "value": "Salaries and wages"
                  },
                  {
                    "id": "2",
                    "value": "100000"
                  }
                ]
              }
            ]
          },
          {
            "key": {
              "unit": "1",
              "field": "1042",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1044",
              "screen": "55"
            },
            "value": "24800",
            "type": "statement",
            "rows": [
              {
                "id": "0",
                "sequence": "1",
                "columns": [
                  {
                    "id": "0",
                    "value": "8"
                  },
                  {
                    "id": "1",
                    "value": "Standard deduction"
                  },
                  {
                    "id": "2",
                    "value": "24800"
                  }
                ]
              }
            ]
          },
          {
            "key": {
              "unit": "1",
              "field": "1048",
              "screen": "55"
            },
            "value": "8632",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1049",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1051",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1053",
              "screen": "55"
            },
            "value": "1000",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1054",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1055",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1056",
              "screen": "55"
            },
            "value": "3600",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1071",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1072",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1074",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1076",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1077",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1078",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1108",
              "screen": "55"
            },
            "value": "4032",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1184",
              "screen": "55"
            },
            "value": "2",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1189",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          },
          {
            "key": {
              "unit": "1",
              "field": "1279",
              "screen": "55"
            },
            "value": "0",
            "type": "field"
          }
        ]
      }
    ]
  }
}

Namespaces

 C#

using System.IO;
using Newtonsoft.Json;

 VB.Net

Imports System.IO
Imports Newtonsoft.Json

Code 

C#

protected void Page_Load(object sender, EventArgs e)
{
    StreamReader stRead = new StreamReader(Server.MapPath("~/json.json"));
    Root root = JsonConvert.DeserializeObject<Root>(stRead.ReadToEnd());
}

public class Key
{
    public string unit { get; set; }
    public string field { get; set; }
    public string screen { get; set; }
}

public class Column
{
    public string id { get; set; }
    public string value { get; set; }
}

public class Row
{
    public string id { get; set; }
    public string sequence { get; set; }
    public List<Column> columns { get; set; }
}

public class ReturnData
{
    public Key key { get; set; }
    public string value { get; set; }
    public string type { get; set; }
    public List<Row> rows { get; set; }
}

public class TaxProduct
{
    public string entity { get; set; }
    public string jurisdiction { get; set; }
    public int taxYear { get; set; }
    public List<ReturnData> returnData { get; set; }
}

public class Data
{
    public string id { get; set; }
    public string keyType { get; set; }
    public string returnStatus { get; set; }
    public string clientId { get; set; }
    public string contactId { get; set; }
    public List<TaxProduct> taxProducts { get; set; }
}

public class Root
{
    public Data data { get; set; }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim stRead As StreamReader = New StreamReader(Server.MapPath("~/json.json"))
    Dim root As Root = JsonConvert.DeserializeObject(Of Root)(stRead.ReadToEnd())
End Sub

Public Class Key
    Public Property unit As String
    Public Property field As String
    Public Property screen As String
End Class

Public Class Column
    Public Property id As String
    Public Property value As String
End Class

Public Class Row
    Public Property id As String
    Public Property sequence As String
    Public Property columns As List(Of Column)
End Class

Public Class ReturnData
    Public Property key As Key
    Public Property value As String
    Public Property type As String
    Public Property rows As List(Of Row)
End Class

Public Class TaxProduct
    Public Property entity As String
    Public Property jurisdiction As String
    Public Property taxYear As Integer
    Public Property returnData As List(Of ReturnData)
End Class

Public Class Data
    Public Property id As String
    Public Property keyType As String
    Public Property returnStatus As String
    Public Property clientId As String
    Public Property contactId As String
    Public Property taxProducts As List(Of TaxProduct)
End Class

Public Class Root
    Public Property data As Data
End Class

Screenshot