{"reels":{"6592252783":{"id":"6592252783","latest_reel_media":1668086478,"expiring_at":1668172878,"seen"....
我們怎么辦 6592252783 每次都在變化。
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class _16278726
{
public string id { get; set; }
public int latest_reel_media { get; set; }
public int expiring_at { get; set; }
public int seen { get; set; }
public bool can_reply { get; set; }
public bool can_gif_quick_reply { get; set; }
public bool can_reshare { get; set; }
public bool can_react_with_avatar { get; set; }
public string reel_type { get; set; }
public object ad_expiry_timestamp_in_millis { get; set; }
public object is_cta_sticker_available { get; set; }
public User user { get; set; }
public List<Item> items { get; set; }
public int prefetch_count { get; set; }
public int media_count { get; set; }
public List<string> media_ids { get; set; }
}
我使用在線 json 到 c# 轉換器。應該如何?
uj5u.com熱心網友回復:
您可以使用JsonDocument來決議提到的 json。然后使用該EnumerateObject方法動態列舉物件的屬性。然后將物件決議為提到的類,如下所示:
var json = @"{""reels"":{""6592252783"":{""id"":""6592252783"",""latest_reel_media"":1668086478,""expiring_at"":1668172878,""seen"":1668172878}}}";
var reels = JsonDocument.Parse(json).RootElement.GetProperty("reels");
var enumerated = reels.EnumerateObject().First(); // first property is the property with the key 6592252783
var obj = enumerated.Value;
var deSerialized = obj.Deserialize<Root>();
小提琴
uj5u.com熱心網友回復:
由于您使用的是 Newtonsoft.Json,您可以在一行中完成
Reel reel = ((JObject)JObject.Parse(json)["reels"]).Properties().First().Value.ToObject<Reel>();
你的班
public class Reel
{
public string id { get; set; }
public int latest_reel_media { get; set; }
public int expiring_at { get; set; }
//....
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/531194.html
標籤:C#json
上一篇:創建一個回傳泛型函式的函式
下一篇:在物體框架中,如何使用System.Data.Entity.EntityState.Modified修改存盤在另一個類中的物件串列?
