Deserialize the JSON where the values are field names with JSON.NET

后端 未结 3 1375
深忆病人
深忆病人 2020-12-11 06:33

I have a very undesirable situation which requires me to deserialize the JSON where the values are field names with JSON.NET. Assuming that I have the following JSON which i

3条回答
  •  臣服心动
    2020-12-11 07:17

    Hm you can try:

    dynamic jObject = JObject.Parse(text);
    List users = new List();
    
    foreach(dynamic dUser in jObject)
    {
        List roles = new List();
        User user = new User();
        user.Name = dUser.name;
        foreach(PropertyInfo info in dUser.GetType().GetProperties())
        {
            Role role = new Role();
            role.Id = info.Name;
            role.Name = dUser[info.Name].name;
            roles.Ad(role);
        }
        user.Roles = roles.ToArray();
    }
    

提交回复
热议问题