gpt4 book ai didi

asp.net - 如何使用 ASP.NET 解析 JSON 字符串?

转载 作者:行者123 更新时间:2023-12-02 07:31:23 25 4
gpt4 key购买 nike

我正在使用 Sendgrid API 发送和检索已发送邮件的统计信息。我想将 API 的响应存储在数据库中。

protected void btnBounces_Click(object sender, EventArgs e)
{
try
{
string url = "https://api.sendgrid.com/api/bounces.get.json";
GetResult(url);
}
catch (Exception ex)
{
lblError.Text = ex.Message.ToString();
}
}
public void GetResult(string url)
{
string parameters = "api_user=xxxx&api_key=xxxx&date=1&start_date="+txtStartDate.Text+"&end_date="+txtEndDate.Text;
// Start the request
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StreamWriter streamWriter = new StreamWriter(req.GetRequestStream());
streamWriter.Write(parameters);
streamWriter.Flush();
streamWriter.Close();
// Get the response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader streamReader = new StreamReader(res.GetResponseStream());
string result = streamReader.ReadToEnd();
}

我会得到这样的回应:

[
{
"status": "4.0.0",
"created": "2011-09-16 22:02:19",
"reason": "Unable to resolve MX host sendgrid.ne",
"email": "esting@sendgrid.ne"
},
{
"status": "4.0.0",
"created": "2011-09-19 17:47:15",
"reason": "Connection timed out",
"email": "rawest@gmail.co"
}
]

如何提取四个字段中的每一个的值并将它们存储在包含四个字段的表中?

最佳答案

如果不想创建Movie类,可以使用System.Web.Script.Serialization解析得到dynamic对象。

JavaScriptSerializer js = new JavaScriptSerializer();
dynamic movie = js.Deserialize<dynamic>(json);

关于asp.net - 如何使用 ASP.NET 解析 JSON 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com