【发布时间】: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