1

I'm currently trying to convert a JSON string into C# objects and currently am experiencing trouble during my debugging. Below is a sample of the JSON along with my classes.

Classes

    public class Timeline_RootObject
    {
        public List<Timeline_Frame> frames { get; set; }
        public int frameInterval { get; set; }
    }

    public class Timeline_Frame
    {
        public Participants players { get; set; }
        public IList<Event> events { get; set; }
        public int timestamp { get; set; }
    }

    public class Participants
    {
        public Players player1{ get; set; }
        public Players player2 { get; set; }
    }

    public class Event_Position
    {
        public int x { get; set; }
        public int y { get; set; }
    }
    public class Player_Position
    {
        public int x { get; set; }
        public int y { get; set; }
    }

    public class Players
    {
        public int participantId { get; set; }
        public Player_Position position { get; set; }
        public int currentGold { get; set; }
    }

    public class Event
    {
        public string type { get; set; }
        public int timestamp { get; set; }
        public int participantId { get; set; }
        public int itemId { get; set; }
    }

Sample JSON

{"frames": [
{
  "participantFrames": {
    "player1": {
      "participantId": 1,
      "position": {
        "x": 561,
        "y": 581
      },
      "currentGold": 475
    },
    {
      "player2": {
        "participantId": 2,
        "position": {
          "x": 561,
          "y": 581
        },
        "currentGold": 475
      }
    },
    "events": [
      {
        "type": "ITEM_PURCHASED",
        "timestamp": 1829,
        "participantId": 1,
        "itemId": 1039
      }
    ],
    "timestamp": 0
  },
  {
    "participantFrames": {
      "player1": {
        "participantId": 1,
        "position": {
          "x": 800,
          "y": 681
        },
        "currentGold": 525
      },
      {
        "player2": {
          "participantId": 2,
          "position": {
            "x": 754,
            "y": 642
          },
          "currentGold": 525
        }
      },
      "events": [
        {
          "type": "ITEM_PURCHASED",
          "timestamp": 45358,
          "participantId": 1,
          "itemId": 684
        }
      ],
      "timestamp": 60000
    }

Currently I am able to access the Event's Type, Timestamp, ItemID and participant ID. But for some reason I can't access classes within a class for example the Event_Position or Players class is always null.

Code Generating Objects

    public List<Timeline_RootObject> json_timeline = new List<Timeline_RootObject>();
    json_timeline.Add(JsonConvert.DeserializeObject<Timeline_RootObject>(json));

If anybody could help guide me through this small roadblock, I'd greatly appreciate it!

2
  • 2
    Okay, and where's your code to generate the objects based on the content of the decoded JSON data? Commented Jun 15, 2015 at 12:50
  • Edited the original entry with the code generating the objects.
    – TaylorM
    Commented Jun 15, 2015 at 12:58

1 Answer 1

1

This property:

public Participants players { get; set; }

doesn't correspond with the key participantFrames in the JSON string. You'll have to change the property name, or add the attribute [JsonProperty("participantFrames")] to it.

Also, the class Event_Position is not used at the moment, nor do I see any occurrence in the JSON.

1
  • I was adapting from my code to not overwhelm the post with un-needed info. Your suggestion of using the [JsonProperty(String)] was the thing I needed. It was such a simple fix, I feel embarassed for even asking from the beginning!
    – TaylorM
    Commented Jun 15, 2015 at 13:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.