Hi 954111rohit,
Check this example. Now please take its reference and correct your code.
HTML
<table>
<tr>
<td>Id</td>
<td>
<asp:TextBox ID="txtId" runat="server" /></td>
</tr>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
</tr>
<tr>
<td>Country</td>
<td><asp:TextBox ID="txtCountry" runat="server" /></td>
</tr>
<tr>
<td colspan="2">
<asp:Button Text="Submit" runat="server" OnClick="OnSubmit" /></td>
</tr>
</table>
Code
C#
protected void OnSubmit(object sender, EventArgs e)
{
Customer customer = new Customer();
customer.Id = Convert.ToInt32(txtId.Text);
customer.Name = txtName.Text;
customer.Country = txtCountry.Text;
// Generate the json string.
string json = Newtonsoft.Json.JsonConvert.SerializeObject(customer, Newtonsoft.Json.Formatting.Indented);
// Write the json string to file.
System.IO.File.WriteAllText(Server.MapPath("~/json.json"), json);
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim customer As Customer = New Customer()
customer.Id = Convert.ToInt32(txtId.Text)
customer.Name = txtName.Text
customer.Country = txtCountry.Text
' Generate the json string.
Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(customer, Newtonsoft.Json.Formatting.Indented)
' Write the json string to file.
IO.File.WriteAllText(Server.MapPath("~/json.json"), json)
End Sub
Public Class Customer
Public Property Id As Integer
Public Property Name As String
Public Property Country As String
End Class
Generated Json file
{
"Id": 1,
"Name": "Mudassar Khan",
"Country": "India"
}