简体   繁体   English

从JSON反序列化期间忽略字段

[英]Ignore field during deserialization from JSON

So let's say i want to take all this data into ac# object with the exception of "totalDamageTaken", what would be best way to do it ? 因此,假设我想将所有这些数据都带到ac#对象中,但“ totalDamageTaken”除外,哪种方法最好呢?

{
   "modifyDate": 1414968686000,
   "champions": [
      {
         "id": 76,
         "stats": {
            "totalDeathsPerSession": 294,
            "totalSessionsPlayed": 44,
            "totalDamageTaken": 1065678,
         }
      },
      {
         "id": 9,
         "stats": {
            "totalDeathsPerSession": 7,
            "totalSessionsPlayed": 1,
            "totalDamageTaken": 45382,
         }
      },
      {
         "id": 10,
         "stats": {
            "totalDeathsPerSession": 65,
            "totalSessionsPlayed": 12,
            "totalDamageTaken": 302252,
         }
      },
      {
         "id": 7,
         "stats": {
            "totalDeathsPerSession": 40,
            "totalSessionsPlayed": 4,
            "totalDamageTaken": 98114,
         }
      }
   ],
   "summonerId": 24609204
}

Am i on the right path? 我在正确的道路上吗?

public class champion
{
    public int id { get; set; }
    public champion (int id)
    {
        this.id = id;
    }

}

public class getStats 
{
    public int id { get; set; }
    public long moddate { get; set; }
    public static int length { get; set; }
    public List<champion> champions;

    public getStats(string json)
    {
        JObject jobject = JObject.Parse(json);
        id = (int)jobject["summonerId"];
        moddate = (long)jobject["modifyDate"];
    }
}

I've tried deserialization, selecttoken, jarrays and all kinds of stuff i've seen. 我已经尝试过反序列化,selecttoken,jarrays和我所见过的各种东西。

Json.NET . Json.NET

Deserialize using JsonConvert.Deserialize(json); 使用JsonConvert.Deserialize(json);反序列化 after adding the [JsonIgnore] attribute to totalDamageTaken . [JsonIgnore]属性添加到totalDamageTaken It's as easy as that. 就这么简单。

Json.NET is a good option like Jeroen Vannevel mentioned, it's much more forgiving than the .NET frameworks JSON deserializers. 就像Je​​roen Vannevel提到的那样,Json.NET是一个不错的选择,它比.NET框架JSON反序列化器要宽容得多。

If you don't want to use an external library you can include a reference to System.Runtime.Serialization in your project (part of the .NET Framework) and use the DataContractJsonSerializer (System.Runtime.Serialization.Json.DataContractJsonSerializer). 如果您不想使用外部库,则可以在项目(.NET Framework的一部分)中包括对System.Runtime.Serialization的引用,并使用DataContractJsonSerializer(System.Runtime.Serialization.Json.DataContractJsonSerializer)。

First decorate your classes with the [DataContract] attribute and each property with the [DataMember] attribute like below. 首先,使用[DataContract]属性装饰类,并使用[DataMember]属性装饰每个属性,如下所示。 Only include the properties you want deserialized (don't include a totalDamageTaken property on the stats class) 仅包含您要反序列化的属性(不包括stats类的totalDamageTaken属性)

[DataContract]
public class root
{
    [DataMember]
    public long modifyDate { get; set; }
    [DataMember]
    public int summonerId { get; set; }
    [DataMember]
    public IEnumerable<champion> champions { get; set; }
}

[DataContract]
public class champion
{
    [DataMember]
    public int id { get; set; }
    [DataMember]
    public stats stats { get; set; }
}

[DataContract]
public class stats
{
    [DataMember]
    public int totalDeathsPerSession { get; set; }
    [DataMember]
    public int totalSessionsPlayed { get; set; }
}

Then call the DataContractJsonSerializer 然后调用DataContractJsonSerializer

// From text
string text = // Json text as string
//// This is kind of a hack, however you do it it's easiest to work with a stream
byte[] chars = text.ToCharArray().Select(c => Convert.ToByte(c)).ToArray();
using (Stream stream = new MemoryStream(chars))
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(root));
    root result = (root)serializer.ReadObject(stream);
}

// From web request
HttpWebResponse request = // Get your request object back from a WebRequest object
using (Stream stream = request.GetResponseStream())
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(root));
    root result = (root)serializer.ReadObject(stream);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM