简体   繁体   中英

How to Save JSON data to SQL server database in C#?

I am using synapse pay API and in return I am getting some response. I want to save that response in SQL database.

I have created classes for that.

Below is code for getting response

 var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://sandbox.synapsepay.com/api/v2/user/create");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"email\":\"aaaanikuaaaanjaa@synapsepay.com\"," +
                              "\"fullname\":\"nik\"," +
                              "\"phonenumber\":\"111\"," +
                              "\"ip_address\":\"1.1.1.1.1\"," +
                              "\"password\":\"123123123\"," +
                              "\"client_id\":\"1111111111111111\"," +
                              "\"client_secret\":\"2222222222222222222\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
        }

My Json Response

"{
\"expires_at\": \"1438381449\", 
\"expires_in\": \"5184000\", 
\"oauth_consumer_key\": \"tDOIKwdgJzSzbCQdpo9FGNCBV6cSDTAlJqdtBHg3\", \"refresh_token\": \"HxJWdwgrNchJHn5zviAO7nd141ALYXmSbNmuG5ZF\",
\"success\": true,
\"user_id\": 10212,
\"username\": \"1433197449c2630fc917ef4d2b846f\
"}"

This is my external_account table

public partial class ExternalAccount
    {
        public ExternalAccount()
           {
                this.UserProfileExternalAccount = new HashSet<UserProfileExternalAccount>();
            }

            public int ExternalAccountId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public string ExternalId { get; set; }
            public string oAuthToken { get; set; }
            public string oAuthTokenSecret { get; set; }
            public string oAuthKey { get; set; }
            public string oAuthSecret { get; set; }
            public string Username { get; set; }
            public string ClientId { get; set; }
            public string ClientSecret { get; set; }
            public string RefreshToken { get; set; }
            public string oAuthConsumerKey { get; set; }
            public bool IsActive { get; set; }
            public System.DateTime WhenAdded { get; set; }
            public System.DateTime WhenUpdated { get; set; }
            public string AddedBy { get; set; }
            public string UpdatedBy { get; set; }
            public int type_ExternalAccountStatusId { get; set; }

            public virtual type_ExternalAccountStatus type_ExternalAccountStatus { get; set; }
            public virtual ICollection<UserProfileExternalAccount> UserProfileExternalAccount { get; set; }
        }
    }

Using above class i have created my table.. I am new to C#, can someone tell me how to parse json and store in database.

If i have written something wrong please correct me..

You can work with JSON Object: https://msdn.microsoft.com/en-us/library/cc197957%28v=vs.95%29.aspx

That will simplify the coding work that you're having.


On the method that you receive the JSON message, you can map the data as below:

var externalAccount = new ExternalAccount();
var receivedResponse  = (JsonObject)JsonObject.Load(responseStream);
externalAccount.ExternalAccountId = receivedResponse["ExternalAccountId"];
externalAccount.Name = receivedResponse["Name"];
...

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