C# 将JSON字符串数组转换为c“List object returning”;空";列表中模型的每个字段的值

C# 将JSON字符串数组转换为c“List object returning”;空";列表中模型的每个字段的值,c#,arrays,json,list,json.net,C#,Arrays,Json,List,Json.net,我有字符串格式的JSON数组列表,并尝试在c#Model列表中进行转换 我正在使用代码的下一行,但它为模型的每个字段返回null值。它在“objectList.count”行中显示正确的计数,但在调试列表数组时,它为模型的每个字段显示“null”值 using System; using System.Collections.Generic; using Newtonsoft.Json; public partial class TestPage : System.Web.UI.Page {

我有字符串格式的JSON数组列表,并尝试在c#Model列表中进行转换

我正在使用代码的下一行,但它为模型的每个字段返回null值。它在“objectList.count”行中显示正确的计数,但在调试列表数组时,它为模型的每个字段显示“null”值

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; }
}
使用系统;
使用System.Collections.Generic;
使用Newtonsoft.Json;
公共部分类测试页:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
ProfileDetailViewModel viewModel=新的ProfileDetailViewModel();
viewModel.ProfileDetailModelList=新列表();
字符串内容=@“[{”“AccountNumber”“:”“1”“CompressedAddress”“:”“1,测试,读取,邮政编码”“,”“MType”“:”“10”“,”“BillNotification”“:”“Y”“,”“Newbillert”“:”“N”“,”“AccountType”“:”“1234568”“}]”;
List objectList=JsonConvert.DeserializeObject(内容);
如果(objectList.Count>0)
{
viewModel.Success=true;
viewModel.ProfileDetailModelList=对象列表;
}
}
}
公共类ProfileDetailViewModel
{
公共列表ProfileDetailModelList{get;set;}
公共bool成功{get;set;}
}
公共类模型
{
字符串AccountNumber{get;set;}
字符串压缩数据{get;set;}
字符串MType{get;set;}
字符串通知{get;set;}
字符串newbillert{get;set;}
字符串AccountType{get;set;}
}

ProfileDetailModel上的所有属性都是私有的。您可以检查类别、结构等的默认可见性。。。在这里:

只需添加
public

public class ProfileDetailModel
{
       public string AccountNumber { get; set; }
       public string CompressedAddress { get; set; }
       public string MType { get; set; }
       public string BillNotification { get; set; }
       public string NewBillAlert { get; set; }
       public string AccountType { get; set; }
}    

您应该将模型类中的所有属性更改为
public

public class ProfileDetailModel
{
    public string AccountNumber { get; set; }
    public string CompressedAddress { get; set; }
    public string MType { get; set; }
    public string BillNotification { get; set; }
    public string NewBillAlert { get; set; }
    public string AccountType { get; set; }
}

将JsonProperty属性添加到所有属性。请参阅下面的屏幕截图

public class ProfileDetailModel
{
    [JsonProperty]
    string AccountNumber { get; set; }

    [JsonProperty]
    string CompressedAddress { get; set; }

    [JsonProperty]
    string MType { get; set; }

    [JsonProperty]
    string BillNotification { get; set; }

    [JsonProperty]
    string NewBillAlert { get; set; }

    [JsonProperty]
    string AccountType { get; set; }
}