Hi Amitabha,
First you need to Deserialize the JSON string to class object after that you need to add necessary data to class object and then convert the class object to JSON.
Please refer below sample.
Namespace
C#
using System.Web.Script.Serialization;
VB.Net
Imports System.Web.Script.Serialization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<Datum> datum = new List<Datum>();
datum.Add(new Datum
{
num = 1,
hsn_sc = "123",
desc = "Test",
uqc = "OTH",
qty = 0,
val = 1481657.69,
txval = 1322908.70,
iamt = 37091.69,
camt = 60828.65,
samt = 60828.65,
csamt = 0
});
Hsn hsn = new Hsn();
hsn.data = datum;
Root root = new Root();
root.hsn = hsn;
string json = new JavaScriptSerializer().Serialize(root);
// Adding new Datum to existing json.
Root data = new JavaScriptSerializer().Deserialize<Root>(json);
data.hsn.data.Add(new Datum
{
num = 2,
hsn_sc = "1234",
desc = "Test 1",
uqc = "OTH 1",
qty = 0,
val = 1481657.69,
txval = 1322908.70,
iamt = 37091.69,
camt = 60828.65,
samt = 60828.65,
csamt = 0
});
json = new JavaScriptSerializer().Serialize(data);
}
public class Root
{
public Hsn hsn { get; set; }
}
public class Hsn
{
public List<Datum> data { get; set; }
}
public class Datum
{
public int num { get; set; }
public string hsn_sc { get; set; }
public string desc { get; set; }
public string uqc { get; set; }
public int qty { get; set; }
public double val { get; set; }
public double txval { get; set; }
public double iamt { get; set; }
public double camt { get; set; }
public double samt { get; set; }
public double csamt { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim datum As List(Of Datum) = New List(Of Datum)()
datum.Add(New Datum With {
.num = 1,
.hsn_sc = "123",
.desc = "Test",
.uqc = "OTH",
.qty = 0,
.val = 1481657.69,
.txval = 1322908.7,
.iamt = 37091.69,
.camt = 60828.65,
.samt = 60828.65,
.csamt = 0
})
Dim hsn As Hsn = New Hsn()
hsn.data = datum
Dim root As Root = New Root()
root.hsn = hsn
Dim json As String = New JavaScriptSerializer().Serialize(root)
Dim data As Root = New JavaScriptSerializer().Deserialize(Of Root)(json)
data.hsn.data.Add(New Datum With {
.num = 2,
.hsn_sc = "1234",
.desc = "Test 1",
.uqc = "OTH 1",
.qty = 0,
.val = 1481657.69,
.txval = 1322908.7,
.iamt = 37091.69,
.camt = 60828.65,
.samt = 60828.65,
.csamt = 0
})
json = New JavaScriptSerializer().Serialize(data)
End Sub
Public Class Root
Public Property hsn As Hsn
End Class
Public Class Hsn
Public Property data As List(Of Datum)
End Class
Public Class Datum
Public Property num As Integer
Public Property hsn_sc As String
Public Property desc As String
Public Property uqc As String
Public Property qty As Integer
Public Property val As Double
Public Property txval As Double
Public Property iamt As Double
Public Property camt As Double
Public Property samt As Double
Public Property csamt As Double
End Class
Generated Json
{
"hsn": {
"data": [
{
"num": 1,
"hsn_sc": "123",
"desc": "Test",
"uqc": "OTH",
"qty": 0,
"val": 1481657.69,
"txval": 1322908.7,
"iamt": 37091.69,
"camt": 60828.65,
"samt": 60828.65,
"csamt": 0
},
{
"num": 2,
"hsn_sc": "1234",
"desc": "Test 1",
"uqc": "OTH 1",
"qty": 0,
"val": 1481657.69,
"txval": 1322908.7,
"iamt": 37091.69,
"camt": 60828.65,
"samt": 60828.65,
"csamt": 0
}
]
}
}