JSON反序列化长列表<long>无法正常工作

时间:2017-07-08 15:02:41

标签: c# json asp.net-mvc json-deserialization

我有一个控制器给JSON作为回应。

public ActionResult Messages()
{
   var Ids = new List<long>() { 1,2,3,4 };
   var data = JsonConvert.SerializeObject(Ids);
   return Json(data, JsonRequestBehavior.AllowGet);
}

此JSON的结果已被回复:

  

[1,2,3,4]

在客户端,我尝试使用以下命令将JSON反序列化回List:

using (_HttpResponse = client.SendAsync(_HttpRequest).Result)
{
    _HttpResponse.EnsureSuccessStatusCode();
    var _ResponseContent = await _HttpResponse.Content.ReadAsStringAsync();
    var Ids = JsonConvert.DeserializeObject<List<long>>(_ResponseContent);
}

_ResponseContent变为:

"[1,2,3,4]"

标记&#34;阵列周围。 在反序列化时,我得到以下异常:

  

转换值时出错&#34; [1,2,3,4]&#34;输入&#39; System.Collections.Generic.List`1 [System.Int64]&#39;。路径&#39;&#39;,第1行,第11位。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package python-quandl 方法将您的数字列表转换为JSON 字符串,然后从您的操作方法返回该字符串。所以基本上你的输出不是真正的数字json数组,而是像这样的字符串

JsonConvert.SerializeObject

您应该只使用返回正确json数组的"[1,2,3,4]" 方法

Json

此方法应该返回这样的响应

public ActionResult Messages()
{
    var Ids = new List<long>() { 1, 2, 3, 4 };
    return Json(Ids, JsonRequestBehavior.AllowGet);
}

通过此响应,您的其他客户端代码应该能够将其正确地反序列化为数字列表