Im trying to convert JSON data from an API to a List. This is what I get from the API:
{
"0":{
"Id":0,
"FNr":"1",
"Len":"1",
"Typ":"1",
"Sort":"1",
"Low":"1",
"Up":"1",
"Rep":"1",
"UC":"1",
"Lo":"1",
"Pf":"1",
"SelP":"1",
"Rel":"1",
"RefLen":"1",
"Description":"1",
"Remarks":"1"
},
"1":{
...
},
"2":{
...
}
}
Now I try to Deserialize this to a List<DBTableEntryModel>
var entries = JSON.Deserialize<List<DbTableEntryModel>>(jsonString);
This fails, because of the numbers. If I remove the numbers of the JSON string, it works perfectly fine. Is there a easier way to fix this issue, without splitting the JSON string?
This is my DBTableEntryModel:
public class DbTableEntryModel
{
[StringLength(3)]
[ColumnWidth("30px")]
public string FNr { get; set; }
[StringLength(5)]
[ColumnWidth("60px")]
public string Len { get; set; }
[StringLength(3)]
[ColumnWidth("30px")]
public string Typ { get; set; }
[StringLength(3)]
[ColumnWidth("35px")]
public string Sort { get; set; }
[StringLength(10)]
[ColumnWidth("100px")]
public string Low { get; set; }
[StringLength(10)]
[ColumnWidth("100px")]
public string Up { get; set; }
[StringLength(1)]
[ColumnWidth("30px")]
public string Rep { get; set; }
[StringLength(1)]
[ColumnWidth("25px")]
public string UC { get; set; }
[StringLength(1)]
[ColumnWidth("25px")]
public string Lo { get; set; }
[StringLength(1)]
[ColumnWidth("25px")]
public string Pf { get; set; }
[StringLength(2)]
[ColumnWidth("34px")]
public string SelP { get; set; }
[ColumnWidth("90px")]
public string Rel { get; set; }
[ColumnWidth("90px")]
public string RefLen { get; set; }
[TextArea]
[Display(Name = "Description", ResourceType = typeof(Resources.Plugin))]
public string Description { get; set; }
[TextArea]
[Display(Name = "Remarks", ResourceType = typeof(Resources.Plugin))]
public string Remarks { get; set; }
}
Instead of deserializing your json into List<DbTableEntryModel>
use Dictionary<string, DbTableEntryModel>
like
var entries = JSON.Deserialize<Dictionary<string, DbTableEntryModel>>(jsonString);
Then you can use foreach loop to get all keys and entry from above dictionary like
foreach (var entry in entries)
{
var key = entry.Key; //"0", "1", "2", ...
var dbTableEntryModel = entry.Value; // Your `DbTableEntryModel` model
}
如果您仍然想使用列表,则可以尝试此操作。
var entries = JSON.Deserialize<Dictionary<string, DbTableEntryModel>>(jsonString).Select(x => x.Value).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.