我正在使用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并存储在数据库中。
如果我写错了,请纠正我..
答案 0 :(得分:4)
您可以使用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"];
...