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来序列化和反序列化? –