【问题标题】:Need help converting JSON to C# Objects需要帮助将 JSON 转换为 C# 对象
【发布时间】:2015-06-15 12:43:38
【问题描述】:

我目前正在尝试将 JSON 字符串转换为 C# 对象,目前在调试过程中遇到了问题。下面是 JSON 的示例以及我的类。

    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; }
    }

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
    }

目前我可以访问事件的类型、时间戳、ItemID 和参与者 ID。但由于某种原因,我无法访问类中的类,例如 Event_Position 或 Players 类始终为空。

代码生成对象

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

如果有人能帮助我解决这个小障碍,我将不胜感激!

【问题讨论】:

  • 好的,根据解码的 JSON 数据的内容生成 objects 的代码在哪里?
  • 用生成对象的代码编辑了原始条目。

标签: c# asp.net json object serialization


【解决方案1】:

这个属性:

public Participants players { get; set; }

与 JSON 字符串中的键 participantFrames 不对应。您必须更改属性名称,或将属性 [JsonProperty("participantFrames")] 添加到其中。

另外,Event_Position 类目前没有使用,我也没有在 JSON 中看到任何情况。

【讨论】:

  • 我正在根据我的代码进行调整,以免用不需要的信息淹没帖子。您对使用 [JsonProperty(String)] 的建议是我需要的。这是一个如此简单的解决方案,我什至从一开始就问我感到尴尬!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多