Convert Json to C# Classes
Online link: https://json2csharp.com/
This is the response you'll get from the JSON request we made earlier:
public class ItmDet
{
public double samt { get; set; }
public int rt { get; set; }
public double txval { get; set; }
public double camt { get; set; }
public double iamt { get; set; }
public double? csamt { get; set; }
}
public class Itm
{
public int num { get; set; }
public ItmDet itm_det { get; set; }
}
public class Inv
{
public List<Itm> itms { get; set; }
public double val { get; set; }
public string inv_typ { get; set; }
public string irn { get; set; }
public string pos { get; set; }
public string srctyp { get; set; }
public string idt { get; set; }
public string rchrg { get; set; }
public string irngendate { get; set; }
public string inum { get; set; }
public string chksum { get; set; }
}
public class B2b
{
public List<Inv> inv { get; set; }
public string cfs { get; set; }
public string ctin { get; set; }
public string cfs3b { get; set; }
public string flprdr1 { get; set; }
}
public class Data
{
public List<B2b> b2b { get; set; }
}
public class Header
{
[JsonProperty("cache-control")]
public string CacheControl { get; set; }
[JsonProperty("content-type")]
public string ContentType { get; set; }
public string gst_username { get; set; }
public string state_cd { get; set; }
public string ip_address { get; set; }
public string txn { get; set; }
public string client_id { get; set; }
public string client_secret { get; set; }
public string ctin { get; set; }
public string authorization { get; set; }
[JsonProperty("postman-token")]
public string PostmanToken { get; set; }
public string ret_period { get; set; }
public string gstin { get; set; }
}
public class Root
{
public Data data { get; set; }
public string status_cd { get; set; }
public string status_desc { get; set; }
public Header header { get; set; }
}
And this is how you deserialize it in your C# code:
Dim result = JsonConvert.DeserializeObject(Of Root)(json)
Private Sub Jsonconverter(ByVal json As String, ByVal RETMonth As String)
DtMain = NewDataTableWithIndentity("GSTNoofSupplier", "SupplierName", "InvNo", "InvDt", "TaxableValue", "TaxValue", _
"InvoiceValue", "CompanyGSTNo", "RETMonth", "isUpdatedOn")
DtMain.Columns("GSTNoofSupplier").DataType = GetType(String)
DtMain.Rows.Clear()
Dim result = JsonConvert.DeserializeObject(Of Root)(json)
Try
If IsNothing(result.data.b2b) Then 'GST website Api perticular month data not found!
Exit Sub
End If
Dim drins As DataRow = Nothing
For i As Integer = 0 To result.data.b2b.Count - 1 ' All Supplier Count
For j As Integer = 0 To result.data.b2b(i).inv.Count - 1 ' Perticular Supplier Total Invoice
drins = DtMain.NewRow
drins.Item("GSTNoofSupplier") = result.data.b2b(i).ctin
drins.Item("InvDt") = result.data.b2b(i).inv(j).idt
drins.Item("invNo") = result.data.b2b(i).inv(j).inum
For k As Integer = 0 To result.data.b2b(i).inv(j).itms.Count - 1 'Perticular Invoice Item Details
drins.Item("TaxableValue") = Val(drins.Item("TaxableValue") & "") + result.data.b2b(i).inv(j).itms(k).itm_det.txval
drins.Item("TaxValue") = Val(drins.Item("TaxValue") & "") + result.data.b2b(i).inv(j).itms(k).itm_det.camt + _
result.data.b2b(i).inv(j).itms(k).itm_det.samt + result.data.b2b(i).inv(j).itms(k).itm_det.iamt
Next
drins.Item("InvoiceValue") = result.data.b2b(i).inv(j).val
drins.Item("CompanyGSTNo") = result.header.gstin
drins.Item("RETMonth") = RETMonth
drins.Item("isUpdatedOn") = Now()
DtMain.Rows.Add(drins)
DtMain.AcceptChanges()
Next
Next
Dim LocationWisePartyPending As String = ""
Dim sb As StringBuilder = New StringBuilder
For Each dr1 As DataRow In DtMain.Rows
For Each dc As DataColumn In DtMain.Columns
sb.Append((WriteCSV(dr1(dc.ColumnName).ToString) + ","))
'End If
Next
sb.Remove((sb.Length - 1), 1)
sb.AppendLine()
Next
Dim temppath As String = Replace(Path.GetTempPath(), "\", "/")
File.WriteAllText(temppath & "GSTR2ATemp.csv", sb.ToString)
ExecuteNonQuery(CommandType.Text, "LOAD DATA LOCAL INFILE '" & temppath & "GSTR2ATemp.csv' " & vbCrLf & _
" INTO TABLE " & "`accountsreportdb`.GSTR2ATemp" & vbCrLf & _
" FIELDS TERMINATED BY ',' " & vbCrLf & _
" ENCLOSED BY '""'" & vbCrLf & _
" LINES TERMINATED BY '\n'" & vbCrLf & _
" IGNORE 0 ROWS;", ConnnectionOption.MainServer)
' Insert records End
ferror = False
Catch ex As Exception
ferror = True
Errorstring = ex.Message
MessageBox.Show(ex.Message, Me.Text)
End Try
End Sub
GSTR2A API CallingDim dictData As New Dictionary(Of String, Object)
Dim webClient As New WebClient()
Dim strResponse As String = ""
webClient.Headers("cache-control") = "no-cache"
webClient.Headers("content-type") = "application/json"
webClient.Headers("gst_username") = dtGSTNoDetails.Rows(i)("gst_username")
webClient.Headers("state_cd") = dtGSTNoDetails.Rows(i)("state_cd")
webClient.Headers("ip_address") = dtGSTNoDetails.Rows(i)("ip_address")
webClient.Headers("txn") = dtGSTNoDetails.Rows(i)("txn")
webClient.Headers("client_id") = dtGSTNoDetails.Rows(i)("client_id")
webClient.Headers("client_secret") = dtGSTNoDetails.Rows(i)("client_secret")
Dim dPoints As Double = 0
Dim data As System.IO.Stream = webClient.OpenRead("https://XXX&gstin=" & dtGSTNoDetails.Rows(i)("gstno") & "&retperiod=" & result(j) & "")
Dim reader As New System.IO.StreamReader(data)
strResponse = reader.ReadToEnd()
data.Close()
reader.Close()
0 Comments