I`m working with VK API. Sometimes server can return empty array instead of object, for example:
personal: [] //when it is empty
or
personal: {
religion: 'Нет',
smoking: 1,
alcohol: 4
} //when not empty.
I`m deserializing most of json with JsonConvert.DeserializeObject, and this part of json with
MainObject = ((MainObject["response"].GetObject())["user"].GetObject())["personal"].GetObject();
try
{
Convert.ToByte(MainObject["political"].GetNumber();
}
catch {}
But it makes app works slowly when it`s handling a lot of exeptions. And just now i realised that here are some more fields that might return array when empty. I just have no ideas how to make it fastly and clearly. Any suggestions?
My deserializing class (doen`t work when field is empty):
public class User
{
//some other fields...
public Personal personal { get; set; }
//some other fields...
}
public class Personal
{
public byte political { get; set; }
public string[] langs { get; set; }
public string religion { get; set; }
public string inspired_by { get; set; }
public byte people_main { get; set; }
public byte life_main { get; set; }
public byte smoking { get; set; }
public byte alcohol { get; set; }
}
Another idea (doesn`t work when not empty):
public List<Personal> personal { get; set; }
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…