gpt4 book ai didi

c# - 来自 RESTful API 的反序列化错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:43:18 24 4
gpt4 key购买 nike

获取错误提示

JsonReaderException: Unexpected character encountered while parsing value: M. Path ', line 0, position 0.

当尝试解析 JSON 响应时(不是全部,但前几项,为了匿名,我在 xxxxxx 中进行了更改):

{
"total_count": 1091,
"items": [
{
"hap": "retry_limit_reached",
"message": "Retry limit reached. Dropped: postmaster@xxxxx.ca \u2192 hamnifold@cxxxxxxxxems.com 'REMINDER - Pattison Stampede BBQ (Jul 11, 2018)' No MX for crscranessystems.com Server response: 498 No MX for crscranessystems.com",
"type": "error",
"created_at": "Thu, 05 Jul 2018 06:45:48 GMT",
"message_id": "20180704185046.1.F263522F7F8571FE@mxxxxxx.ca"
},
{
"hap": "tempfailed",
"message": "Will retry in 14400 seconds: postmaster@xxxxxxxx.ca \u2192 conor@xxxxxxxxxy.org 'REMINDER - Pattison Stampede BBQ (Jul 11, 2018)' No MX for greexxxxxxxy.org Server response: 498 No MX for grexxxxxxy.org",
"type": "warn",
"created_at": "Thu, 05 Jul 2018 06:45:17 GMT",
"message_id": "20180704225015.1.E917B878FC275EE7@mxxxxxxx.ca"
},
{
"hap": "opened",
"message": "Opened: jordan@lixxxxxxxxs.com",
"type": "info",
"created_at": "Thu, 05 Jul 2018 06:45:15 GMT",
"message_id": "20180704192612.1.856579D071F84CF8@xxxxxxx.ca"
},
{
"hap": "tempfailed",
"message": "Will retry in 14400 seconds: postmaster@mxxxxxxx.ca \u2192 ccrawford@xxxxxx.com 'SUBJECT' No MX for step-eps.com Server response: 498 No MX for stxxxxxx.com",
"type": "warn",
"created_at": "Thu, 05 Jul 2018 06:45:14 GMT",
"message_id": "20180704225014.1.33D047AC8A685236@xxxxxxxxi.ca"
},
//...

这是我的部分代码:

if(response.IsSuccessStatusCode)
{
ViewBag.Msg = "Success";
var stresult = await response.Content.ReadAsStringAsync();
var flogs = JsonConvert.DeserializeObject<List<EmailLog>>(stresult);
logs = flogs.ToList();
}

这是电子邮件日志模型:

public class EmailLog
{
public string hap { get; set; }

public string message { get; set; }

public string type { get; set; }

public string created_at { get; set; }

public string message_id { get; set; }
}

这是我第一次使用 RESTful API 和 JSON,所以我可能会遗漏一些简单的东西。

编辑:

这是我发出 API 请求的完整 Controller 代码:

HttpClient client = new HttpClient();

client.BaseAddress = new Uri("https://api:key-xxxxxxxxxxxxxxxxxxxxxxxxxxxx@api.mailgun.net/v3");

List<EmailLog> logs = new List<EmailLog>();

HttpResponseMessage response = await client.GetAsync("/mg.xxxxx.ca/log");
if (response.IsSuccessStatusCode)
{

// var stresult = await response.Content.ReadAsStringAsync();
// var flogs = JsonConvert.DeserializeObject<List<EmailLog>>(stresult);
// logs = flogs.ToList();
var json = await response.Content.ReadAsStringAsync();
var jObject = JObject.Parse(json);
if (jObject.ContainsKey("items"))
{
ViewBag.Msg = "Success";
logs = jObject["items"].ToObject<List<EmailLog>>();
}
//var logs = JsonConvert.DeserializeObject<ApiResponse>(json);

}

else { ViewBag.Msg = "Fail"; }

return View(logs);

最佳答案

问题是试图直接获取 EmailLog 对象的列表,因为它们包含在您的 json 响应的“items”元素中。

可以使用包装类来解决,例如:

public class ApiResponse
{
[JsonProperty(PropertyName = "total_count")]
public int TotalCount { get; set; }
[JsonProperty(PropertyName = "items")]
public List<EmailLog> Items { get; set; }
}

然后你不需要使用列表来反序列化:

var flogs = JsonConvert.DeserializeObject<ApiResponse>(json);

关于c# - 来自 RESTful API 的反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51185262/

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