我从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”:空, “ACCOUNT_STATUS”:空, “CREATE_DATE”:空, “CUSTOMER_ID”:9000415, “电子邮件”:空 “将First_Name”:” IMAD”, “Last4SSN”:空, “LastPayment_Amount”:空, “姓氏”: “卡玛”, “Last_Payment_Date”:空, “MODIFIED_DATE”:空, “Online_Agreement”:假的, “Payment_in”:空, “用户名” :空, “isActivated”:假的, “isActive”:假的, “isLocked”:假 “isRegistered”:假的, “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对象反序列化的数组或List . JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化 . 路径'GetAllCustomersResult',第1行,第25位 .
1 回答
您的json不是数组或列表 . 它是一个单一的对象 . 所以你的代码应该是这样的
顺便说一句:我使用http://json2csharp.com/从你的json创建c#类 .