提问人:dashley 提问时间:8/27/2023 最后编辑:SSSdashley 更新时间:9/2/2023 访问量:70
JSON 向类添加值
JSON Adding values to a class
问:
有点新(没有经验)的 JSON。我正在尝试在我的项目中添加一个附件代码。示例代码为“HAZ”。
我为 Accessorials 创建了一个公共类(见下文),在该类中,我有“作为 String() 的公共代码”。
Public Class Accessorials
Public codes As String()
Public hazardousContact As HazardousContact
Public cod As Object
Public insuranceDetails As Object
Public sortAndSegregateDetails As Object
Public markDetails As Object
End Class
这反过来又为主包装器提供食物
Public Class fgtload 'Primary wrapper
Public service As New fgtservice
Public payment As New fgtpayment
Public transit As New fgttransit
Public commodities As New List(Of Items)
Public origin As New fgtorigin
Public destination As New fgtdestination
Public billTo As New fgtbillto
**Public accessorials As New Accessorials**
End Class
我正在尝试将代码“HAZ”添加到Accessorials类中。
它将代码显示为“公共代码作为 String()”
,我通过 () 假设它正在寻找各种数组。
(在主包装器中)附件是否应该是一个列表(附件)?
结果应该是:
"accessorials" : {
"codes" : [ "HAZ" ],
"hazardousContact" : {
"name" : "John Snow",
"phone" : "7704865900"
},
以下是 API 指南中的完整 JSON
{"service":{"level":"STND"},"payment":{"terms":"Prepaid","payer":"Shipper"},"transit":{"pickupDate":"20230930"},"commodities":[{"classification":"92.5","weight":"1500","length":"48","width":"40","height":"40","pieces":"3","packagingType":"PAT","description":"Umbrellas","stackable":"Y"},{"classification":"50","weight":"50","length":"48","width":"48","height":"48","pieces":"5","packagingType":"PAT","description":"Soda","stackable":"N"}],"accessorials":{"codes":["HAZ"],"hazardousContact":{"name":"John Snow","phone":"7704865900"},"cod":null,"insuranceDetails":null,"sortAndSegregateDetails":null,"markDetails":null},"origin":{"account":"1234567","city":"LEBANON","stateProvince":"TN","postalCode":"37090","country":"USA"},"destination":{"account":"","city":"Calgary","stateProvince":"AB","postalCode":"T1Y2S2","country":"CAN"},"billTo":{"account":"","name":"Bill To Company","address":"456 Bill Me Rd.","city":"LEBANON","stateProvince":"TN","postalCode":"37090","country":"USA"}}
答:
0赞
SSS
8/28/2023
#1
不确定为什么以下方法不起作用?
Dim f = new fgtload
ReDim Preserve f.accessorials.codes(f.accessorials.codes.Length)
f.accessorials.codes(f.accessorials.codes.Length - 1) = "HAZ"
评论
0赞
dashley
8/29/2023
嗨,谢谢你的回复。你从哪里得到f.accessorials中的“f”?
0赞
SSS
8/29/2023
抱歉,这是你的班级。我已经更新了我的答案f
fgtload
0赞
dashley
8/29/2023
这是我:)的猜测。我已经在代码的顶部声明了“加载为新的fgtload”。所以我把 Load 换成了你的 f。 ReDim Preserve Load.accessorials.codes(Load.accessorials.codes.Length) Load.accessorials.codes(Load.accessorials.codes.Length - 1) = “HAZ” 现在我收到一个错误:“对象引用未设置为对象的实例。使用您提交的或我修改的您的。
0赞
dashley
9/2/2023
#2
决议是:
Load.accessorials.codes = 新列表(字符串) Load.accessorials.codes.Add(“IDL”) -或者用这个 Load.accessorials.codes = 新列表(字符串)({“IDL”})
似乎我实际使用的代码,来自实际的 API 指南,是无效的,即拼写错误。我已经联系了他们的网络支持并报告了错别字。一旦我开始使用正确的代码,我就能够毫无错误地处理 JSON。
我发现代码很糟糕,进一步消耗了 httpresponse 使用:
Dim sReader = New StreamReader(webExcp.Response.GetResponseStream)
Dim result = sReader.ReadToEnd
result = result
通过这样做,我看到了有关提交的代码无效的错误。 我不知道使用更大的错误代码的能力。我通常只是抓住统计数据和简单的消息。生活和学习。反正我现在很好。感谢您的所有意见。
上一个:传单地图和 VB.net
评论