【发布时间】:2017-07-31 19:16:30
【问题描述】:
我有字符串格式的 JSON 数组列表,并尝试在 c# 模型列表中进行转换。
我正在使用下面的代码行,但它为 Model 的每个字段返回 null 值。它在“objectList.Count”行中显示正确的计数,但在调试列表数组时显示“null” Model 的每个字段的值。
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ProfileDetailViewModel viewModel = new ProfileDetailViewModel();
viewModel.ProfileDetailModelList = new List<ProfileDetailModel>();
string content = @"[{""AccountNumber"":""1"",""CompressedAddress"":""1, TEST, READING, Postcode"",""MType"":""10"",""BillNotification"":""Y"",""NewBillAlert"":""N"",""AccountType"":""1234568""}]";
List<ProfileDetailModel> objectList = JsonConvert.DeserializeObject<List<ProfileDetailModel>>(content);
if (objectList.Count > 0)
{
viewModel.Success = true;
viewModel.ProfileDetailModelList = objectList;
}
}
}
public class ProfileDetailViewModel
{
public List<ProfileDetailModel> ProfileDetailModelList { get; set; }
public bool Success { get; set; }
}
public class ProfileDetailModel
{
string AccountNumber { get; set; }
string CompressedAddress { get; set; }
string MType { get; set; }
string BillNotification { get; set; }
string NewBillAlert { get; set; }
string AccountType { get; set; }
}
【问题讨论】:
标签: c# arrays json list json.net