我在創建請求時遇到了一些問題。
還有另一個 API 具有這樣的請求模式:
{
"someNumber":"111222333",
"theList":{
"0":"1233",
"1":"2333"
},
"something":"thisThing"
}
我的問題是theList. 如何在我的 API 的請求中模擬這些索引?我所做的是以下內容:
public class Item
{
[JsonProperty("someNumber")]
public long Number{ get; set; }
[JsonProperty("theList")]
public List<string> NumberList{ get; set; }
[JsonProperty("something")]
public string Something{ get; set; }
}
但這顯然是不正確的,模擬其他 API 中所做的事情的最佳方法是什么?
uj5u.com熱心網友回復:
的 json 結構theList可以反序列化為Dictionary<string,string>:
public class Item
{
[JsonProperty("someNumber")]
public long Number{ get; set; }
[JsonProperty("theList")]
public Dictionary<string, string> NumberList{ get; set; }
[JsonProperty("something")]
public string Something{ get; set; }
}
演示
如果鍵和值theList始終是數字,因為你使用Netwontoft.Json,你也可以反序列化直入Dictionary<int, int>,Dictionary<int, long>,Dictionary<string, long>,等。然而,這不會作業System.Text.Json。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/396157.html
上一篇:如何將JSON轉換為字串陣列
