【发布时间】:2015-03-13 21:03:44
【问题描述】:
我从 MVC5 项目中调用了一个 Rest wcf 服务。我尝试序列化 Jason 以将其发送到查看,但出现错误。 这是我调用服务并返回 Json 的控制器代码。
public class CustomerController : Controller
{
readonly string customerServiceUri = "http://localhost:63674/CSA.svc/";
// GET: /Customer/
public ActionResult Index()
{
object customerList = null;
using (WebClient webclient = new WebClient())
{
string jsonStr;
jsonStr = webclient.DownloadString(customerServiceUri + "GetAllCustomers");
customerList = JsonConvert.DeserializeObject<List<Customer>>(jsonStr);
}
return View(customerList);
}
jsonStr 变量具有以下值:
{"GetAllCustomersResult":[{"Account_Number":"7000019000415","Account_Payment_Status":null,"Account_Status":null,"Create_Date":null,"Customer_ID":9000415,"Email":null,"First_Name ":"IMAD","Last4SSN":null,"LastPayment_Amount":null,"Last_Name":"KAMAR","Last_Payment_Date":null,"Modified_Date":null,"Online_Agreement":false,"Payment_in":null, "用户名":null,"isActivated":false,"isActive":false,"isLocked":false,"isRegistered":false,"unsuccessful_login_count":0},
但我在消毒线中遇到错误,这是错误:
无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型“System.Collections.Generic.List`1[CSAMVCCALLService.Models.Customer]”,因为该类型需要 JSON 数组(例如[1,2,3]) 正确反序列化。 要修复此错误,要么将 JSON 更改为 JSON 数组(例如 [1,2,3]),要么将反序列化类型更改为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径“GetAllCustomersResult”,第 1 行,位置 25。
【问题讨论】:
标签: java c# json wcf serialization