0
我試圖張貼到使用RestSharp(http://restsharp.org/)序列化vb.net類成JSON與RestSharp
一個RESTful API我的類看起來是這樣的:
Public Class CreatePostcard
Public Property [to]() As [To]
Public Property front() As String
...
Public Class [to]
Public Property address_line1 As string
Public Property city As String
...
我的要求是這樣的:
Dim request = New RestRequest("postcards", Method.POST)
Dim mm = New CreatePostcard() With {.description = "Test Desc",
.to = New [To]() With {.name = FirstName & " " & LastName, .address_line1 = Address1, .address_line2 = Address2, .address_city = City, .address_state = State, .address_zip = Zip},
.from = New From() With {.name = "TestFrom", .address_line1 = "123 Fake St", .address_city = "Bentonville", .address_state = "AK", .address_zip = "90210"},
.back = BackURL, .front = FrontURL, .message = ""}
request.AddObject(mm)
Dim response2 As IRestResponse(Of Postcard) = client.Execute(Of Postcard)(request)
的API期待的東西,看起來像這樣:
{
"description": "Demo Postcard job",
"to": {
"address_line1": "123 Test Street",
"address_city": "Mountain View",
"address_state": "CA",
"address_zip": "94041",
},
"from": {
"name": "Ami Wang",
"address_line1": "123 Test Avenue"
},
"message": null,
}
但是實際上,我在送是:
{
"to": "MyApp.To",
"from": "MyApp.From",
"front": "http://foo",
"back": "http://foo",
"message": "",
"description": "Test"
}
如何正確seralize的對象,並將其發送到寧靜的API?
您需要將To和From列出。你只是發送一個字符串。你需要一個數組或列表,這也會反映在你的json中。爲什麼不使用newtonsoft來序列化和反序列化? –