简体   繁体   中英

Get rest API data and save them into SQL Table with C#

Here is what I am looking for. I need to get data from a rest api and insert it into my SQL table. i have searched here and on google for a solution but they never matched on my needs. Im very new to coding so it is difficult for me to understand all the steps.

Here is what i have so far:

  1. I try to call the api (works)
  2. to fill the data in a DataTable (NOT WORKING)
  3. to insert them in a SQL table

Here my code:

protected void btnAPI_OnClick(object sender, EventArgs e)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072;

            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential(username,password);
                string json = client.DownloadString("https://XXXX/XXXX001?view=expand");
                JObject jArray = JObject.Parse(json);
                var dt = JsonConvert.DeserializeAnonymousType(json, new { Content = default(DataTable)}).Content; //On this point the code gets an error and it stops


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    connection(); //This is my connection string. It works!
                    sqlConn.Open();
                    using (sqlConn)
                    {
                        string qry = "INSERT INTO myTable VALUES(@name, @age, @date)";
                        connection();
                        sqlConn.Open();

                        using (SqlCommand cmd = new SqlCommand(qry, sqlConn))
                        {
                            cmd.Parameters.AddWithValue("@name", dt.Rows[i]["name"].ToString());
                            cmd.Parameters.AddWithValue("@age", dt.Rows[i]["age"].ToString());
                            cmd.Parameters.AddWithValue("@date", dt.Rows[i]["date"].ToString());

                            cmd.ExecuteNonQuery();

                        }
                        sqlConn.Close();
                    }
                }
            }

Here the json:

{
  "@count": 2,
  "@start": 1,
  "@totalcount": 2,
  "Messages": [],
  "ResourceName": "user",
  "ReturnCode": 0,
  "content": [
    {"user": {
      "name": "Eric",
      "age": "25",
      "date": "2021-10-23T11:18:10+00:00",
    }},
    {"user": {
      "name": "Paul",
      "age": "30",
      "date": "2021-10-23T11:18:10+00:00",
    }}]
}

Here the error: Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable: StartObject. Path 'content[0].user', linke 9, position 20.' Newtonsoft.Json.JsonSerializationException: 'Unexpected JSON token when reading DataTable: StartObject. Path 'content[0].user', linke 9, position 20.'

I think that the issue is that the json im getting has under content a atribute with sub attributes. but i have no idea how i can solve this.

thank you very much for your help.

Regards, Edi

First, remove the connection declaration from a loop because your application would suffer a performance degradation from that (maxed out Pool)

Secondly, it is best you cast the response from the API to a object; then it will make your work easier to complete.

Now, the issue could be,

  1. the datatype from the API for name parameter isn't a string. Therefore you need to align that with the column attribute type

  2. Make sure the JSON is properly formatted. It should be string:string eg "name":"stackoverflow"

I tried to map the json to an object

public List<UserModel> GetUserList()
        {
            try
            {
                using (WebClient webClient = new WebClient())
                {
                    webClient.Credentials = new NetworkCredential("un", "pw");
                    webClient.BaseAddress = StaticItems.EndPoint;
                    var json = webClient.DownloadString("userlist?view=expand");
                    var list = JsonConvert.DeserializeObject < List<UserModel>>(json);
                    return list.ToList();
                }
            }
            catch (WebException ex)
            {
                throw ex;
            }
        }

public static class StaticItems
{
    public static string EndPoint = "https://XXXXXX";
}

Here is the model:

namespace APIWebApp.Models
{
    public class UserModel
    {
        public int count { get; set; }
        public int start { get; set; }
        public int totalcount { get; set; }
        public object[] Messages { get; set; }
        public string ResourceName { get; set; }
        public int ReturnCode { get; set; }
        public Content[] content { get; set; }
    }
    
    public class Content
    {
        public Users user { get; set; }
    }

    public class Users
    {
        public string name { get; set; }
        public string age { get; set; }
        public string date { get; set; }
    }
}

The API json:

{
  "@count": 2,
  "@start": 1,
  "@totalcount": 2,
  "Messages": [],
  "ResourceName": "user",
  "ReturnCode": 0,
  "content": [
    {"user": {
      "name": "Eric",
      "age": "25",
      "date": "2021-10-23T11:18:10+00:00",
    }},
    {"user": {
      "name": "Paul",
      "age": "30",
      "date": "2021-10-23T11:18:10+00:00",
    }}]
}

The ERROR msg:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List'1[APIWebApp.Models.UserModel]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '@count', line 2, position 11.'

i realy have no idea what Im doing wrong.

Thank you

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