1

So let's say i want to take all this data into a c# object with the exception of "totalDamageTaken", what would be best way to do it ?

{
   "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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I've changed title a bit to highlight need to ignore one field... and post... - feel free to improve/revert. – Alexei Levenkov Nov 07 '14 at 00:47
  • Normally it would be duplicate of http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp, but it looks like your question is very specific about not reading a field. – Alexei Levenkov Nov 07 '14 at 00:48

2 Answers2

1

Json.NET.

Deserialize using JsonConvert.Deserialize(json); after adding the [JsonIgnore] attribute to totalDamageTaken. It's as easy as that.

Timigen
  • 1,055
  • 1
  • 17
  • 33
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

Json.NET is a good option like Jeroen Vannevel mentioned, it's much more forgiving than the .NET frameworks JSON deserializers.

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).

First decorate your classes with the [DataContract] attribute and each property with the [DataMember] attribute like below. Only include the properties you want deserialized (don't include a totalDamageTaken property on the stats class)

[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

// 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);
}
Timigen
  • 1,055
  • 1
  • 17
  • 33