Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Consume Web API: Copy Multidimensional Array Values into SQL DB

$
0
0

Hello People,

Continuing from my previous threads, assuming I have the following JSON response:

"Items": [
{
"LocalTimestamp": "2017-07-18T17:27:36+08:00",
"Id": 658796613,
"Description": "Speeding",
"Processed": false,
"Position": [
101.57393,
3.22654
]
}
],

How do I access the multidimensional array named Position (Bolded above) and copy the two values into two separate columns named x-post and y-post in SQL DB.??Undecided
So far I have managed to copy other values listed in the JSON response via SQLBulkCopy (Please see previous thread for reference). But I can't seem to have those two values which is in the form of [,]. Here's what I have done so far.

I've created class as such 

    public class MainPage
    {
        public int Page { get; set; }
        public int PageSize { get; set; }
        public int TotalResults { get; set; }
        public List<Alarm> Items { get; set; }
        public bool HasMoreResults { get; set; }
    }

    public class Alarm
    {
        public String LocalTimeStamp { set; get; }
        public int Id { set; get; }
        public string Description { set; get; }
        public string Processed { set; get; }
        public string[] Position { set; get; }
    }

A data table for BulkCopy process

                DataTable ATable = new DataTable();
                ATable.Columns.Add(new DataColumn("LocalTimeStamp"));
                ATable.Columns.Add(new DataColumn("Id"));
ATable.Columns.Add(new DataColumn("Description")); ATable.Columns.Add(new DataColumn("Processed")); ATable.Columns.Add(new DataColumn("Position"));

and the main program to pull and deserialize the data 

                foreach (var item in car.Items)
                {
                    string buzzers = mzoneweb.DownloadString("https://us.gpscar.net/api/v2/vehicles/" + "d10n" + "/alerts/all/" + Date + "T000100/" + Date + "T235900.json?");
                    AlertPageDetails alert = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<AlertPageDetails>(buzzers);
                    foreach (var list in alert.Items)
                    {
                        //Console.WriteLine("LocalTimeStamp: {0}, Id: {1}, Description: {2}, Processed: {3}", list.LocalTimeStamp, list.Id, list.Description, list.Processed);
                        DataRow TempRow = ATable.NewRow();
                        TempRow["LocalTimeStamp"] = list.LocalTimeStamp;
                        TempRow["Id"] = list.Id;
                        TempRow["Description"] = list.Description;
                        TempRow["Processed"] = list.Processed;
                        ATable.Rows.Add(TempRow);
                        foreach (var val in list.Position)
                        {
                            //Console.WriteLine("Position: {0}", val.ToString());
                            TempRow["Position"] = val.ToString();
                            //ATable.Rows.Add(TempRow);
                        }
                    }
                }

Thanks. 

Regards,
Dein


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>