Hi maideem,
First you need to create a class based on json data. Then get the data from database and serialize it using JavaScriptSerializer.
Please refer below sample.
HTML
<asp:Label ID="lblJson" runat="server"></asp:Label>
Namespaces
C#
using System.IO;
using System.Web.Script.Serialization;
VB.Net
Imports System.IO
Imports System.Web.Script.Serialization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<Shipment> ship = new List<Shipment>();
ShipmentAddressFrom addressFrom = new ShipmentAddressFrom
{
CompanyName = "ABC ENTERPRISE",
UnitNumber = "6, ",
Address = "Jalan TP2, "
};
ShipmentAddressTo addressTo = new ShipmentAddressTo
{
CompanyName = "XYZ Enterprise",
UnitNumber = "10, ",
Address = "Jalan TP5, "
};
List<WayBill> wayBills = new List<WayBill>();
wayBills.Add(new WayBill
{
WayBillNo = ""
});
List<InsurancePurchase> insurancePurchases = new List<InsurancePurchase>();
insurancePurchases.Add(new InsurancePurchase
{
ProductDescription = "The description of the item .",
Quantity = 1,
UnitPrice = 110
});
ship.Add(new Shipment
{
ShipmentServiceType = "Standard Delivery",
SenderName = "ABC",
RecipientName = "XYZ",
DONumber = "",
ShipmentAddressFrom = addressFrom,
ShipmentAddressTo = addressTo,
WayBill = wayBills,
InsurancePurchase = insurancePurchases
});
Root root = new Root { Shipment = ship };
JavaScriptSerializer serializer = new JavaScriptSerializer();
lblJson.Text = serializer.Serialize(root);
}
public class InsurancePurchase
{
public string ProductDescription { get; set; }
public int Quantity { get; set; }
public int UnitPrice { get; set; }
}
public class Root
{
public List<Shipment> Shipment { get; set; }
}
public class Shipment
{
public string ShipmentServiceType { get; set; }
public string SenderName { get; set; }
public string RecipientName { get; set; }
public ShipmentAddressFrom ShipmentAddressFrom { get; set; }
public ShipmentAddressTo ShipmentAddressTo { get; set; }
public List<InsurancePurchase> InsurancePurchase { get; set; }
public List<WayBill> WayBill { get; set; }
public string DONumber { get; set; }
}
public class ShipmentAddressFrom
{
public string CompanyName { get; set; }
public string UnitNumber { get; set; }
public string Address { get; set; }
}
public class ShipmentAddressTo
{
public string CompanyName { get; set; }
public string UnitNumber { get; set; }
public string Address { get; set; }
}
public class WayBill
{
public string WayBillNo { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ship As List(Of Shipment) = New List(Of Shipment)()
Dim addressFrom As ShipmentAddressFrom = New ShipmentAddressFrom With {
.CompanyName = "ABC ENTERPRISE",
.UnitNumber = "6, ",
.Address = "Jalan TP2, "
}
Dim addressTo As ShipmentAddressTo = New ShipmentAddressTo With {
.CompanyName = "XYZ Enterprise",
.UnitNumber = "10, ",
.Address = "Jalan TP5, "
}
Dim wayBills As List(Of WayBill) = New List(Of WayBill)()
wayBills.Add(New WayBill With {
.WayBillNo = ""
})
Dim insurancePurchases As List(Of InsurancePurchase) = New List(Of InsurancePurchase)()
insurancePurchases.Add(New InsurancePurchase With {
.ProductDescription = "The description of the item .",
.Quantity = 1,
.UnitPrice = 110
})
ship.Add(New Shipment With {
.ShipmentServiceType = "Standard Delivery",
.SenderName = "ABC",
.RecipientName = "XYZ",
.DONumber = "",
.ShipmentAddressFrom = addressFrom,
.ShipmentAddressTo = addressTo,
.WayBill = wayBills,
.InsurancePurchase = insurancePurchases
})
Dim root As Root = New Root With {
.Shipment = ship
}
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
lblJson.Text = serializer.Serialize(root)
End Sub
Public Class InsurancePurchase
Public Property ProductDescription As String
Public Property Quantity As Integer
Public Property UnitPrice As Integer
End Class
Public Class Root
Public Property Shipment As List(Of Shipment)
End Class
Public Class Shipment
Public Property ShipmentServiceType As String
Public Property SenderName As String
Public Property RecipientName As String
Public Property ShipmentAddressFrom As ShipmentAddressFrom
Public Property ShipmentAddressTo As ShipmentAddressTo
Public Property InsurancePurchase As List(Of InsurancePurchase)
Public Property WayBill As List(Of WayBill)
Public Property DONumber As String
End Class
Public Class ShipmentAddressFrom
Public Property CompanyName As String
Public Property UnitNumber As String
Public Property Address As String
End Class
Public Class ShipmentAddressTo
Public Property CompanyName As String
Public Property UnitNumber As String
Public Property Address As String
End Class
Public Class WayBill
Public Property WayBillNo As String
End Class
Output
Generated Json
{
"Shipment": [
{
"ShipmentServiceType": "Standard Delivery",
"SenderName": "ABC",
"RecipientName": "XYZ",
"ShipmentAddressFrom": {
"CompanyName": "ABC ENTERPRISE",
"UnitNumber": "6, ",
"Address": "Jalan TP2, "
},
"ShipmentAddressTo": {
"CompanyName": "XYZ Enterprise",
"UnitNumber": "10, ",
"Address": "Jalan TP5, "
},
"InsurancePurchase": [
{
"ProductDescription": "The description of the item .",
"Quantity": 1,
"UnitPrice": 110
}
],
"WayBill": [ { "WayBillNo": "" } ],
"DONumber": ""
}
]
}