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

4
我正在使用 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并存储到数据库中。

如果我写错了什么,请纠正我。

1个回答

5
你可以使用JSON对象进行操作: https://msdn.microsoft.com/zh-cn/library/cc197957(v=vs.95).aspx 这将简化你的编码工作。
在接收JSON消息的方法中,你可以按照以下方式映射数据:
var externalAccount = new ExternalAccount();
var receivedResponse  = (JsonObject)JsonObject.Load(responseStream);
externalAccount.ExternalAccountId = receivedResponse["ExternalAccountId"];
externalAccount.Name = receivedResponse["Name"];
...

我在答案中添加了更多信息,我相信那就是你要找的。 - Thiago Avelino

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接