简体   繁体   English

如何在C#中将JSON数据保存到SQL Server数据库?

[英]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. 我正在使用synapse pay API,作为回报,我得到了一些回应。 I want to save that response in SQL database. 我想在SQL数据库中保存该响应。

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 我的Json回应

"{
\"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 这是我的external_account表

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. 使用上面的类我创建了我的表..我是C#的新手,有人可以告诉我如何解析json并存储在数据库中。

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 您可以使用JSON对象: 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: 在您收到JSON消息的方法上,您可以映射数据,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c#中将字典保存和访问到SQL Server数据库中 - how to save and access Dictionary into SQL Server Database in c# 如何在C#中的SQL Server数据库中保存CheckBox值 - How to save CheckBox value in SQL Server database in C# 如何使用 C# 在 SQL 服务器数据库中保存 DataGridViewComboBoxColumn - How save DataGridViewComboBoxColumn in SQL Server database using C# 如何使用C#将BiFir数据保存到sql数据库? - How to save this biFir data to sql database using c#? 如何将datagridview批量数据保存到sql数据库,而datagridview数据与图像绑定到c#中的SQL数据库 - how to save datagridview bulk data to sql database while the datagridview data binded with images to SQL database in c# 如何在 C# 中从 SQL Server 数据库中检索数据? - How to retrieve data from a SQL Server database in C#? C# 我需要什么数据类型才能将 JSON 文件保存为 SQL 数据库中的字段? - C# What data type do I need to save a JSON file as a field in a SQL database? 为什么我的SQL Server数据库不使用过程保存数据-C# - Why does my SQL Server database do not save the data using a procedure - C# Excel文件使用asp.net C#读取数据并将其保存在SQL Server数据库中 - Excel file read and save data in SQL Server database using asp.net c# 来自Google文档的Excel使用asp.net C#读取数据并将其保存在SQL Server数据库中 - Excel from Google Docs read and save data in SQL Server database using asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM