Hi maideen,
Please refer below sample.
HTML
<asp:label ID="lblJsonData" runat="server"></asp:label>
Namespaces
C#
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Web.Script.Serialization;
VB.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Converters
Imports System.Web.Script.Serialization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
List<Root> root = new List<Root>();
PickupAddress PickupAddress = new PickupAddress()
{
name = "xxy Company LTD",
address1 = "1, 11,13, Jalan Rajawali",
address2 = "Bandar Puchong Jaya",
city = "Puchong",
country = "MY",
district = "Puchong",
state = "Selangor",
postCode = "47000",
email = "sales@xxy.com",
phone = "069076 1651"
};
ShipperAddress shipperAddress = new ShipperAddress()
{
name = "xxy Company LTD",
address1 = "1, 11,13, Jalan Rajawali",
address2 = "Bandar Puchong Jaya",
city = "Puchong",
country = "MY",
district = "Puchong",
state = "Selangor",
postCode = "47000",
email = "sales@xxy.com",
phone = "069076 1651"
};
ConsigneeAddress consignement = new ConsigneeAddress()
{
name = "Poliklinik Gunwant",
address1 = "Lot 9797, Jalan Menteri",
address2 = "Sungai Besar Selangor",
city = "Sungai Besar",
state = "Selangor",
district = "Sungai Besar",
country = "MY",
postCode = "46000",
email = "",
phone = "092548 3257"
};
ValueAddedService valADDService = new ValueAddedService() { vasCode = "PPOD" };
ValueAddedServices valueAddedServices = new ValueAddedServices() { valueAddedService = new List<ValueAddedService>() { valADDService } };
List<ShipmentPiece> shipmentPieces = new List<ShipmentPiece>();
shipmentPieces.Add(new ShipmentPiece() { pieceID = 1 });
List<ShipmentItem> shipItem = new List<ShipmentItem>();
shipItem.Add(new ShipmentItem()
{
consigneeAddress = consignement,
shipmentID = "123456789",
packageDesc = "abc",
totalWeight = '1',
totalWeightUOM = "KG",
dimensionUOM = "CM",
productCode = "PDO",
currency = "USD",
remarks = "",
isMult = "true",
deliveryOption = "C",
shipmentPieces = shipmentPieces,
valueAddedServices = valueAddedServices
});
Hdr hdr = new Hdr()
{
accessToken = "123456789",
messageDateTime = new DateTime(2023, 01, 01, 11, 20, 27),
messageLanguage = "en",
messageType = "SHIPMENT",
messageVersion = "1.4"
};
Bd bd = new Bd()
{
pickupAccountId = "123587",
soldToAccountId = "325784",
pickupDateTime = new DateTime(2023, 01, 01, 11, 20, 27),
handoverMethod = '1',
pickupAddress = PickupAddress,
shipperAddress = shipperAddress,
shipmentItems = shipItem
};
ManifestRequest manifesto = new ManifestRequest()
{
bd = bd,
hdr = hdr
};
root.Add(new Root()
{
manifestRequest = manifesto
});
JavaScriptSerializer serializer = new JavaScriptSerializer();
string v_jsonData = "";
v_jsonData = serializer.Serialize(root);
this.lblJsonData.Text = JsonConvert.SerializeObject(root, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
}
public class Bd
{
public string pickupAccountId { get; set; }
public string soldToAccountId { get; set; }
public DateTime pickupDateTime { get; set; }
public int handoverMethod { get; set; }
public PickupAddress pickupAddress { get; set; }
public ShipperAddress shipperAddress { get; set; }
public List<ShipmentItem> shipmentItems { get; set; }
}
public class ConsigneeAddress
{
public string name { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string district { get; set; }
public string country { get; set; }
public string postCode { get; set; }
public string phone { get; set; }
public string email { get; set; }
}
public class Hdr
{
public string messageType { get; set; }
public DateTime messageDateTime { get; set; }
public string accessToken { get; set; }
public string messageVersion { get; set; }
public string messageLanguage { get; set; }
}
public class ManifestRequest
{
public Hdr hdr { get; set; }
public Bd bd { get; set; }
}
public class PickupAddress
{
public string name { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string district { get; set; }
public string country { get; set; }
public string postCode { get; set; }
public string phone { get; set; }
public string email { get; set; }
}
public class Root
{
public ManifestRequest manifestRequest { get; set; }
}
public class ShipmentItem
{
public ConsigneeAddress consigneeAddress { get; set; }
public string shipmentID { get; set; }
public string packageDesc { get; set; }
public int totalWeight { get; set; }
public string totalWeightUOM { get; set; }
public string dimensionUOM { get; set; }
public string productCode { get; set; }
public string currency { get; set; }
public string remarks { get; set; }
public string isMult { get; set; }
public string deliveryOption { get; set; }
public List<ShipmentPiece> shipmentPieces { get; set; }
public ValueAddedServices valueAddedServices { get; set; }
}
public class ValueAddedService
{
public string vasCode { get; set; }
}
public class ValueAddedServices
{
public List<ValueAddedService> valueAddedService { get; set; }
}
public class ShipmentPiece
{
public int pieceID { get; set; }
}
public class ShipperAddress
{
public string name { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string district { get; set; }
public string country { get; set; }
public string postCode { get; set; }
public string phone { get; set; }
public string email { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim root As List(Of Root) = New List(Of Root)()
Dim PickupAddress As PickupAddress = New PickupAddress() With {
.name = "xxy Company LTD",
.address1 = "1, 11,13, Jalan Rajawali",
.address2 = "Bandar Puchong Jaya",
.city = "Puchong",
.country = "MY",
.district = "Puchong",
.state = "Selangor",
.postCode = "47000",
.email = "sales@xxy.com",
.phone = "069076 1651"
}
Dim shipperAddress As ShipperAddress = New ShipperAddress() With {
.name = "xxy Company LTD",
.address1 = "1, 11,13, Jalan Rajawali",
.address2 = "Bandar Puchong Jaya",
.city = "Puchong",
.country = "MY",
.district = "Puchong",
.state = "Selangor",
.postCode = "47000",
.email = "sales@xxy.com",
.phone = "069076 1651"
}
Dim consignement As ConsigneeAddress = New ConsigneeAddress() With {
.name = "Poliklinik Gunwant",
.address1 = "Lot 9797, Jalan Menteri",
.address2 = "Sungai Besar Selangor",
.city = "Sungai Besar",
.state = "Selangor",
.district = "Sungai Besar",
.country = "MY",
.postCode = "46000",
.email = "",
.phone = "092548 3257"
}
Dim valADDService As ValueAddedService = New ValueAddedService() With {
.vasCode = "PPOD"
}
Dim valueAddedServices As ValueAddedServices = New ValueAddedServices() With {
.valueAddedService = New List(Of ValueAddedService)() From {
valADDService
}
}
Dim shipmentPieces As List(Of ShipmentPiece) = New List(Of ShipmentPiece)()
shipmentPieces.Add(New ShipmentPiece() With {
.pieceID = 1
})
Dim shipItem As List(Of ShipmentItem) = New List(Of ShipmentItem)()
shipItem.Add(New ShipmentItem() With {
.consigneeAddress = consignement,
.shipmentID = "123456789",
.packageDesc = "abc",
.totalWeight = "1",
.totalWeightUOM = "KG",
.dimensionUOM = "CM",
.productCode = "PDO",
.currency = "USD",
.remarks = "",
.isMult = "true",
.deliveryOption = "C",
.shipmentPieces = shipmentPieces,
.valueAddedServices = valueAddedServices
})
Dim hdr As Hdr = New Hdr() With {
.accessToken = "123456789",
.messageDateTime = New DateTime(2023, 1, 1, 11, 20, 27),
.messageLanguage = "en",
.messageType = "SHIPMENT",
.messageVersion = "1.4"
}
Dim bd As Bd = New Bd() With {
.pickupAccountId = "123587",
.soldToAccountId = "325784",
.pickupDateTime = New DateTime(2023, 1, 1, 11, 20, 27),
.handoverMethod = "1",
.pickupAddress = PickupAddress,
.shipperAddress = shipperAddress,
.shipmentItems = shipItem
}
Dim manifesto As ManifestRequest = New ManifestRequest() With {
.bd = bd,
.hdr = hdr
}
root.Add(New Root() With {
.manifestRequest = manifesto
})
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim v_jsonData As String = ""
v_jsonData = serializer.Serialize(root)
Me.lblJsonData.Text = JsonConvert.SerializeObject(root, New IsoDateTimeConverter() With {
.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
})
End Sub
Public Class Bd
Public Property pickupAccountId As String
Public Property soldToAccountId As String
Public Property pickupDateTime As DateTime
Public Property handoverMethod As Integer
Public Property pickupAddress As PickupAddress
Public Property shipperAddress As ShipperAddress
Public Property shipmentItems As List(Of ShipmentItem)
End Class
Public Class ConsigneeAddress
Public Property name As String
Public Property address1 As String
Public Property address2 As String
Public Property city As String
Public Property state As String
Public Property district As String
Public Property country As String
Public Property postCode As String
Public Property phone As String
Public Property email As String
End Class
Public Class Hdr
Public Property messageType As String
Public Property messageDateTime As DateTime
Public Property accessToken As String
Public Property messageVersion As String
Public Property messageLanguage As String
End Class
Public Class ManifestRequest
Public Property hdr As Hdr
Public Property bd As Bd
End Class
Public Class PickupAddress
Public Property name As String
Public Property address1 As String
Public Property address2 As String
Public Property city As String
Public Property state As String
Public Property district As String
Public Property country As String
Public Property postCode As String
Public Property phone As String
Public Property email As String
End Class
Public Class Root
Public Property manifestRequest As ManifestRequest
End Class
Public Class ShipmentItem
Public Property consigneeAddress As ConsigneeAddress
Public Property shipmentID As String
Public Property packageDesc As String
Public Property totalWeight As Integer
Public Property totalWeightUOM As String
Public Property dimensionUOM As String
Public Property productCode As String
Public Property currency As String
Public Property remarks As String
Public Property isMult As String
Public Property deliveryOption As String
Public Property shipmentPieces As List(Of ShipmentPiece)
Public Property valueAddedServices As ValueAddedServices
End Class
Public Class ValueAddedService
Public Property vasCode As String
End Class
Public Class ValueAddedServices
Public Property valueAddedService As List(Of ValueAddedService)
End Class
Public Class ShipmentPiece
Public Property pieceID As Integer
End Class
Public Class ShipperAddress
Public Property name As String
Public Property address1 As String
Public Property address2 As String
Public Property city As String
Public Property state As String
Public Property district As String
Public Property country As String
Public Property postCode As String
Public Property phone As String
Public Property email As String
End Class
Generated json
[
{
"manifestRequest": {
"hdr": {
"messageType": "SHIPMENT",
"messageDateTime": "2023-01-01 11:20:27",
"accessToken": "123456789",
"messageVersion": "1.4",
"messageLanguage": "en"
},
"bd": {
"pickupAccountId": "123587",
"soldToAccountId": "325784",
"pickupDateTime": "2023-01-01 11:20:27",
"handoverMethod": 49,
"pickupAddress": {
"name": "xxy Company LTD",
"address1": "1, 11,13, Jalan Rajawali",
"address2": "Bandar Puchong Jaya",
"city": "Puchong",
"state": "Selangor",
"district": "Puchong",
"country": "MY",
"postCode": "47000",
"phone": "069076 1651",
"email": "sales@xxy.com"
},
"shipperAddress": {
"name": "xxy Company LTD",
"address1": "1, 11,13, Jalan Rajawali",
"address2": "Bandar Puchong Jaya",
"city": "Puchong",
"state": "Selangor",
"district": "Puchong",
"country": "MY",
"postCode": "47000",
"phone": "069076 1651",
"email": "sales@xxy.com"
},
"shipmentItems": [
{
"consigneeAddress": {
"name": "Poliklinik Gunwant",
"address1": "Lot 9797, Jalan Menteri",
"address2": "Sungai Besar Selangor",
"city": "Sungai Besar",
"state": "Selangor",
"district": "Sungai Besar",
"country": "MY",
"postCode": "46000",
"phone": "092548 3257",
"email": ""
},
"shipmentID": "123456789",
"packageDesc": "abc",
"totalWeight": 49,
"totalWeightUOM": "KG",
"dimensionUOM": "CM",
"productCode": "PDO",
"currency": "USD",
"remarks": "",
"isMult": "true",
"deliveryOption": "C",
"shipmentPieces": [
{
"pieceID": 1
}
],
"valueAddedServices": {
"valueAddedService": [
{
"vasCode": "PPOD"
}
]
}
}
]
}
}
}
]