{"reels":{"6592252783":{"id":"6592252783","latest_reel_media":1668086478,"expiring_at":1668172878,"seen"....
How can we do 6592252783 is changing every time.
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; }
}
ı use online json to c# converter .How should it be?
CodePudding user response:
Since you are using Newtonsoft.Json you can do it in one line
Reel reel = ((JObject)JObject.Parse(json)["reels"]).Properties().First().Value.ToObject<Reel>();
your class
public class Reel
{
public string id { get; set; }
public int latest_reel_media { get; set; }
public int expiring_at { get; set; }
//....
}
CodePudding user response:
You can use JsonDocument
to parse the mentioned json. And then use the EnumerateObject
method to dynamically enumerate the object's properties. And then parse the object into mentioned class like below :
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>();