[ad_1]
put リクエストに渡すことができる JSON 文字列を作成できないようです。また、json オブジェクト内に配列を作成できません。 助けてください
私が試したこと:
VB
Dim payload As Dictionary(Of String, String) = New Dictionary(Of String, String)() payload.Add("session", "AM") payload.Add("complexOrderStrategyType", "NONE") payload.Add("price", "1000") payload.Add("orderType", "LIMIT") payload.Add("duration", "DAY") payload.Add("orderStrategyType", "Single") payload.Add("specialInstruction", "ALL_OR_NONE") Dim orderLegCollection As Dictionary(Of String, String) = New Dictionary(Of String, String)(payload) orderLegCollection.Add("instruction", "BUY") orderLegCollection.Add("quantity", "1") orderLegCollection.Add("quantityType", "SHARES") Dim instrument As Dictionary(Of String, String) = New Dictionary(Of String, String)(orderLegCollection) instrument.Add("symbol", "MSFT") instrument.Add("assetType", "EQUITY") Dim strPayload As String = JsonConvert.SerializeObject(payload) Dim c As HttpContent = New StringContent(strPayload, Encoding.UTF8, "application/json") Dim t = Task.Run(Function() PostURI(u, c)) t.Wait()
解決策 1
の payload
プロパティのクラスである必要がある場合、実際のペイロードの文字列を生成する JSON オブジェクトにエンコードできます。
1 つの解決策は、サンプル/サンプル JSON ペイロードをダウンロードし、チャット GPT などのツールを使用して VB をクラスに変換することです。
私の入力は次のとおりです。
convert this JSON to VB.NEt classes. Use Camel case and JsonProperty attribute. now use the sample data and show how to convert from class to JSON. { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": { "batter": [ { "id": "1001", "type": "Regular" }, { "id": "1002", "type": "Chocolate" }, { "id": "1003", "type": "Blueberry" }, { "id": "1004", "type": "Devil's Food" } ] }, "topping": [ { "id": "5001", "type": "None" }, { "id": "5002", "type": "Glazed" }, { "id": "5005", "type": "Sugar" }, { "id": "5007", "type": "Powdered Sugar" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5003", "type": "Chocolate" }, { "id": "5004", "type": "Maple" } ] }
そして、生成されたクラスは次のとおりです。
VB.NET
Imports Newtonsoft.Json Public Class Batter <JsonProperty("id")> Public Property Id As String <JsonProperty("type")> Public Property Type As String End Class Public Class Topping <JsonProperty("id")> Public Property Id As String <JsonProperty("type")> Public Property Type As String End Class Public Class Donut <JsonProperty("id")> Public Property Id As String <JsonProperty("type")> Public Property Type As String <JsonProperty("name")> Public Property Name As String <JsonProperty("ppu")> Public Property Ppu As Decimal <JsonProperty("batters")> Public Property Batters As New Dictionary(Of String, List(Of Batter)) <JsonProperty("topping")> Public Property Topping As List(Of Topping) End Class
そしてサンプルコード:
VB.NET
Imports Newtonsoft.Json Module Module1 Sub Main() ' Creating a sample Donut object with data Dim donutObject As New Donut With { .Id = "0001", .Type = "donut", .Name = "Cake", .Ppu = 0.55D, .Batters = New Dictionary(Of String, List(Of Batter)) From { {"batter", New List(Of Batter) From { New Batter With {.Id = "1001", .Type = "Regular"}, New Batter With {.Id = "1002", .Type = "Chocolate"}, New Batter With {.Id = "1003", .Type = "Blueberry"}, New Batter With {.Id = "1004", .Type = "Devil's Food"} }} }, .Topping = New List(Of Topping) From { New Topping With {.Id = "5001", .Type = "None"}, New Topping With {.Id = "5002", .Type = "Glazed"}, New Topping With {.Id = "5005", .Type = "Sugar"}, New Topping With {.Id = "5007", .Type = "Powdered Sugar"}, New Topping With {.Id = "5006", .Type = "Chocolate with Sprinkles"}, New Topping With {.Id = "5003", .Type = "Chocolate"}, New Topping With {.Id = "5004", .Type = "Maple"} } } ' Converting the Donut object to JSON Dim jsonStr As String = JsonConvert.SerializeObject(donutObject, Formatting.Indented) ' Displaying the JSON string Console.WriteLine(jsonStr) End Sub End Module
[ad_2]
コメント