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

标签 c# asp.net .net sql-server json

我正在使用 synapse pay API,作为返回,我得到了一些回应。我想将该响应保存在 SQL 数据库中。

我已经为此创建了类。

下面是获取响应的代码

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

我的 Json 响应

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

这是我的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; }
        }
    }

使用上面的类我已经创建了我的表.. 我是 C# 的新手,有人可以告诉我如何解析 json 并将其存储在数据库中。

如果我写的不对,请指正。

最佳答案

您可以使用 JSON 对象: https://msdn.microsoft.com/en-us/library/cc197957%28v=vs.95%29.aspx

这将简化您的编码工作。


在您接收JSON消息的方法上,您可以将数据映射如下:

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

关于c# - 如何在 C# 中将 JSON 数据保存到 SQL Server 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584555/

相关文章:

c# - .NET 中的安全 Azure 移动服务 - 如何获取 userId

c# - BindingList.Add() 即使有锁也不能跨线程工作

C# 在执行计算时丢失小数分辨率

c# - 如何使用 Azure.Messaging.ServiceBus 库添加自定义属性?

asp.net - 使用 Webforms 和 MVC 路由到项目上的 aspx 页面

asp.net - Azure 的 HttpRuntime.Cache 设置?

c# - 异步方法中的多个等待

asp.net - Sitecore 获取渲染的 Html

c# - 在您的应用程序中加载任何页面时是否有调用的函数?

.net - 以编程方式在桌面上创建 MS SQL Compact 数据库文件