生成的json类型值无法转换为system.collections.generic.list时如何解决这个问题? 。

[ad_1]

你好。

我在生成 Json 时出错。 错误是“ValueAddedService”类型的值无法转换为 System.Collections.Generic.List(Of ValueAddedService)’
另外,日期和时间是“messageDateTime =”\/Date(1674459281468)\/”,不是像“2023-01-19T11:20:27+08:00”这样的日期时间格式
请就上述两个问题给我建议

谢谢

马丁

我尝试过的:

VB
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 List(Of ValueAddedServices) = New List(Of ValueAddedServices)()
 valueAddedServices.Add(New ValueAddedServices With {.valueAddedService = 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
 })

 Dim hdr As Hdr = New Hdr With {
 .accessToken ="123456789",
 .messageDateTime ="\/Date(1674459281468)\/",
 .messageLanguage = "en",
 .messageType = "SHIPMENT",
 .messageVersion = "1.4"
 }
 Dim bd As Bd = New Bd With {
     .pickupAccountId = "123587",
     .soldToAccountId = "325784",
     .pickupDateTime = "\/Date(1674459281468)\/",
     .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()
 v_jsonData = ""
 v_jsonData = serializer.Serialize(root)
 Me.txtJsonData.Text = serializer.Serialize(root)

结果应该如下。

{
	"manifestRequest": {
		"hdr": {
			"messageType": "SHIPMENT",
			"messageDateTime": "2023-01-19T11:20:27+08:00",
			"accessToken": "f0108fb771fa4d269e10d8702f487b93",
			"messageVersion": "1.4",
			"messageLanguage": "en"
		},
		"bd": {
			"pickupAccountId": "5264574522",
			"soldToAccountId": "5264574522",
			"pickupDateTime": "2023-01-19T11:20:27+08:00",
			"handoverMethod": 1,
			"pickupAddress": {
				"name": "Jet Pharam",
				"address1": "adddress1",
				"address2": "address2 .",
				"city": "puchong",
				"state": "selangor",
				"district": "puchong",
				"country": "MY",
				"postCode": "46000",
				"phone": "123456789",
				"email": "abc@jetpharma.com"
			},
			"shipperAddress": {
				"name": "Jet Pharam",
				"address1": "Address1",
				"address2": "Address2",
				"city": "Puchong",
				"state": "Selangor",
				"district": "Puchong",
				"country": "MY",
				"postCode": "46000",
				"phone": "123456789",
				"email": "abc@jetpharma.com"
			},
			"shipmentItems": [
				{
					"consigneeAddress": {
						"name": "Poliklinik Gunwant",
						"address1": "Lot 9797, Jalan Menteri",
						"address2": "Lot 9797, Jalan Menteri",
						"city": "Sungai Besar",
						"state": "Selangor",
						"district": "Sungai Besar",
						"country": "MY",
						"postCode": "45300",
						"phone": "123456789",
						"email": "abc@xxx.com"
					},
					"shipmentID": "MYJMS346524660",
					"packageDesc": "asdasad",
                    "totalWeight": 1,
					"totalWeightUOM": "G",
					"dimensionUOM": "CM",
					"productCode": "PDO",
					"currency": "MYR",
					"remarks": "JETPharma",
					"isMult": "true",
					"valueAddedServices": {
						"valueAddedService": [
							{
								"vasCode": "PPOD"
							}
						]
					},
					"deliveryOption": "C",
					"shipmentPieces": [
						{
							"pieceID": 11
						}
					]
				}
			]
		}
	}
}

解决方案1

通过 JSON 类转换器运行您的代码,它将生成以下结构:

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 valueAddedServices As ValueAddedServices
    Public Property deliveryOption As String
    Public Property shipmentPieces As List(Of ShipmentPiece)
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

Public Class ValueAddedService
    Public Property vasCode As String
End Class

Public Class ValueAddedServices
    Public Property valueAddedService As List(Of ValueAddedService)
End Class

因此,JSON 反序列化期望返回 Root 的实例,其中包含 ManifestRequest,而不是包含在子类中的集合。

当您反序列化 JSON 时,您将获得整个数据 – 而不仅仅是您可能感兴趣的部分!

解决方案3

您正在尝试反序列化 ValueAddedService 的单个实例并将其传递到 API 中。 如果您使用的是单个实例,那就没问题,但实际上您反序列化为一个列表,并将其发送到 API。

解决方案2

[
{
"Version":"1.1",
"TranDtls":{
               "TaxSch":"GST",
               "SupTyp":"B2B",
               "IgstOnIntra":"N",
               "RegRev":"N",
               "EcmGstin":null
},
"DocDtls":{
               "Typ":"INV",
               "No":"1952",
               "Dt":"22/02/2024"
},
"SellerDtls":{
               "Gstin":"29ACUPU9257A1ZT",
               "LglNm":"DORESWAMY VELMURUGAN",
               "TrdNm":"SWARNALAKSHMI",
               "Addr1":"MUTHU COMPLEX, H B ROAD,",
               "Addr2":"HEGGADADEVANAKOTE",
               "Loc":"HEGGADADEVANAKOTE",
               "Pin":571114,
               "Stcd":"29",
               "Ph":"9242889917",
               "Em":"thegreatvelu@gmail.com"
},
"BuyerDtls":{
               "Gstin":"32CEHPB9957C1ZM",
               "LglNm":"GOLDEN INTERLOCK",
               "TrdNm":"GOLDEN INTERLOCK",
               "Pos":"32",
               "Addr1":"1/11, valliyoorkavu",
               "Addr2":"mananthavady",
               "Loc":"mananthavady",
               "Pin":670645,
               "Stcd":"32",
               "Ph":null,
               "Em":null
},
"ValDtls":{
               "AssVal":44687,
               "IgstVal":12512,
               "CgstVal":0,
               "SgstVal":0,
               "CesVal":0,
               "StCesVal":0,
               "Discount":0,
               "OthChrg":0,
               "RndOffAmt":0,
               "TotInvVal":57199
},
                         "ItemList":[{
                                "SlNo":"1",
                                "PrdDesc":"Tyre",
                                "IsServc":"N",
                                "HsnCd":"40112010",
                                "Barcde":null,
                                "Qty":2,
                                "Unit":"NOS",
                                "UnitPrice":22343.50,
                                "TotAmt":44687,
                                "Discount":0,
                                "PreTaxVal":0,
                                "AssAmt":44687,
                                "GstRt":28,
                                "IgstAmt":12512,
                                "CgstAmt":0,
                                "SgstAmt":0,
                                "CesRt":0,
                                "CesAmt":0,
                                "CesNonAdvlAmt":0,
                                "StateCesRt":0,
                                "StateCesAmt":0,
                                "StateCesNonAdvlAmt":0,
                                "OthChrg":0,
                                "TotItemVal":57199
                              }
                              ]
               }
]

[ad_2]

コメント

标题和URL已复制