尝试反序列化JSON这样:JavaScriptSerializer - 如何在名称中使用破折号(“ - ”)反序列化属性?
{
"result":"success"
"arguments": {
"activeTorrentCount":22,
"cumulative-stats": {
"downloadedBytes":1111,
}
}
}
我的类:
private class DeserializationMain
{
public string result; //works
public args arguments; //works, has deserialized activeTorrentCount
public class args
{
public int activeTorrentCount;
public current cumulative_stats; //doesn't work, equals null
public class current
{
public long downloadedBytes;
}
}
}
我猜累积统计数据不被反序列化,因为它在我的课,如何反序列化的东西有cumulative_stats变量名用短划线?
不这样做的 - 大多数.NET编码指南将它作为CumulativeStats。如果它是私人成员var,则为_cumulativeStats或m_cumulativeStats。 – bryanmac
@bryanmac:我认为JSON格式完全超出了他的控制范围。 – BalusC
我相信这取决于您选择的JSON序列化库的功能,但是在一些C#属性中,您可以使用它来定义与C#类名称不同的映射名称的字符串。 –