使用 JavaScriptSerializer 反序列化 JSON
Deserialize JSON using JavaScriptSerializer
我在 JSON 反序列化方面遇到了问题。我的 JSON 字符串格式如下:
{
"event": [
[
{
"Id": 456895,
"Name": "Chelsea - Arsenal",
"BetOffers": [
{
"BetType": "Game",
"Picks": [
{
"Pick": "1",
"Odds": 1.15
},
{
"Pick": "x",
"Odds": 1.46
},
{
"Pick": "2",
"Odds": 1.15
}
]
}
]
}
],
[
{
"Id": 456879,
"Name": "Liverpool - Manchester United",
"BetOffers": [
{
"BetType": "Game",
"Picks": [
{
"Pick": "1",
"Odds": 1.20
},
{
"Pick": "x",
"Odds": 1.42
},
{
"Pick": "2",
"Odds": 1.85
}
]
}
]
}
]
]
}
我根据这个创建了类:
public class Event
{
public int Id { get; set; }
public string Name { get; set; }
public List<BetOffer> BetOffers { get; set; }
}
public class BetOffer
{
public string BetType { get; set; }
public List<BetPick> Picks { get; set; }
}
public class BetPick
{
public string Pick { get; set; }
public double Odds { get; set; }
}
当我反序列化 JSON 字符串时,我得到的是空列表。这就是我进行反序列化的方式:
var jsonString = File.ReadAllText(filePath);
var result = new JavaScriptSerializer().Deserialize<List<Event>>(jsonString);
结果中的值为空列表(计数 = 0)。我哪里做错了?有没有更好的方法从这个 JSON 创建一个对象?
你的模型应该有点不同。
public class PickItem
{
public string Pick { get; set; }
public double Odds { get; set; }
}
public class BetOffer
{
public string BetType { get; set; }
public List<PickItem> Picks { get; set; }
}
public class BetPick
{
public int Id { get; set; }
public string Name { get; set; }
public List<BetOffer> BetOffers { get; set; }
}
public class MyRootObject
{
public List<List<BetPick>> @event { get; set; }
}
var root = new JavaScriptSerializer().Deserialize<MyRootObject>(jsonString);
我在 JSON 反序列化方面遇到了问题。我的 JSON 字符串格式如下:
{
"event": [
[
{
"Id": 456895,
"Name": "Chelsea - Arsenal",
"BetOffers": [
{
"BetType": "Game",
"Picks": [
{
"Pick": "1",
"Odds": 1.15
},
{
"Pick": "x",
"Odds": 1.46
},
{
"Pick": "2",
"Odds": 1.15
}
]
}
]
}
],
[
{
"Id": 456879,
"Name": "Liverpool - Manchester United",
"BetOffers": [
{
"BetType": "Game",
"Picks": [
{
"Pick": "1",
"Odds": 1.20
},
{
"Pick": "x",
"Odds": 1.42
},
{
"Pick": "2",
"Odds": 1.85
}
]
}
]
}
]
]
}
我根据这个创建了类:
public class Event
{
public int Id { get; set; }
public string Name { get; set; }
public List<BetOffer> BetOffers { get; set; }
}
public class BetOffer
{
public string BetType { get; set; }
public List<BetPick> Picks { get; set; }
}
public class BetPick
{
public string Pick { get; set; }
public double Odds { get; set; }
}
当我反序列化 JSON 字符串时,我得到的是空列表。这就是我进行反序列化的方式:
var jsonString = File.ReadAllText(filePath);
var result = new JavaScriptSerializer().Deserialize<List<Event>>(jsonString);
结果中的值为空列表(计数 = 0)。我哪里做错了?有没有更好的方法从这个 JSON 创建一个对象?
你的模型应该有点不同。
public class PickItem
{
public string Pick { get; set; }
public double Odds { get; set; }
}
public class BetOffer
{
public string BetType { get; set; }
public List<PickItem> Picks { get; set; }
}
public class BetPick
{
public int Id { get; set; }
public string Name { get; set; }
public List<BetOffer> BetOffers { get; set; }
}
public class MyRootObject
{
public List<List<BetPick>> @event { get; set; }
}
var root = new JavaScriptSerializer().Deserialize<MyRootObject>(jsonString);