简体   繁体   中英

Deserialize json string using JavaScriptSerializer

I am working on deserialize json string using JavaScriptSerializer in C#, and trying to parse the json string and map it to the appropriate columns in the sql server table for inserting data. I have the sample json string as below.

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

Based on the json output string I am writing my class in C# as below.

Classes

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

public class MyRootObject
{
    public List<List<BetPick>> @event { get; set; }
}
    
var root = new JavaScriptSerializer().Deserialize<MyRootObject>(jsonString);

Insert data into the table as following.

string connectionString = "Database ConnectionString";

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("BetType", typeof(string)));
dt.Columns.Add(new DataColumn("Pick", typeof(string)));
dt.Columns.Add(new DataColumn("Odds", typeof(string)));
DataRow dr = dt.NewRow();

for (var i = 0; i < root.event.Count; i++)
{
    dr = dt.NewRow();
    dr["ID"] = root.event[i].Id;//stuck at table to json string parse and map
    dt.Rows.Add(dr);
}

Question

I'm stuck with how to parse json string and map data to appropriate columns in the table for inserting data into a sql table. Unable to find root.event.Id from the json string?

Error

List does not contain definition for Id

@event property must be of type List<List<Event>> .

public class MyRootObject
{
    public List<List<Event>> @event { get; set; }
}
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; }
}

Let's iterate through this structure. Since you used a for loop, I also made code on for loops (of course, this code can be greatly simplified).

for (int i = 0; i < root.@event.Count; i++)
{
    for (int j = 0; j < root.@event[i].Count; j++)
    {
        Console.WriteLine("Id: " + root.@event[i][j].Id);
        Console.WriteLine("Name: " + root.@event[i][j].Name);

        for (int k = 0; k < root.@event[i][j].BetOffers.Count; k++)
        {
            Console.WriteLine("BetType: " + root.@event[i][j].BetOffers[k].BetType);

            for (int l = 0; l < root.@event[i][j].BetOffers[k].Picks.Count; l++)
            {
                Console.WriteLine(root.@event[i][j].BetOffers[k].Picks[l].Pick +
                    "  " + root.@event[i][j].BetOffers[k].Picks[l].Odds);
            }
        }
        Console.WriteLine();
    }
}

As you can see, there are four nested loops. A DataTable is a square matrix that can be traversed by two loops.

The data structure represented by your json is not flat. Therefore, it cannot be represented as a single DataTable .

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