我从第三方API收到一个有点不可预测的JSON结构。例如,我从这样的课程开始:
public Class UserTuple
{
public int uid {get; set;}
public String email {get; set;}
public Dictionary<string,int> stats {get; set;}
//Unknown structure here (Although I know its name)...
}
我可能会为此创建一个类,但它会很嵌套......我不在乎这部分的价值观,所以这似乎是一种浪费。有没有办法让 JSON.NET 知道忽略这个未知部分?
PS:我以这种方式反序列化:
JsonConvert.Deserialize<List<UserTuple>>(receivedJSON);
如果你不关心它,似乎你只需要适当地设置JsonSerializerSettings.MissingMemberHandling
:
var settings = new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
};
var tuples = JsonConvert.DeserialiazeObject<List<UserTuple>>(json, settings);