简体   繁体   中英

Deserialize JSON using JavaScriptSerializer

I'm having trouble with JSON deserialization. I have JSON string formated like this:

{
    "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
                            }
                        ]
                    }
                ]
            }
        ]
    ]
}

I have created classes according to this:

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

When I'm deserializing that JSON string I end up with empty list. This is how I'm doing deserialization:

var jsonString = File.ReadAllText(filePath);
var result = new JavaScriptSerializer().Deserialize<List<Event>>(jsonString);

Value inside my result is empty list (Count = 0). Where did I go wrong? Is there a better way to create an object from this JSON?

Your model should be a little bit different.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM