0

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?

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, that really helped me out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.