将JSON字符串数组转换为c# List对象,返回列表中模型的每个字段的“null”值

时间:2022-09-25 15:30:59

I have list of JSON array in string format,and trying to convert in List of c# Model.

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

I am using below line of code but its returning null value for each field of Model.Its showing correct count in line "objectList.Count" but when debugging the list array its showing "null" value for every field of Model.

我在代码行下面使用它,但是它为模型的每个字段返回空值。它在“objectList”行中显示正确的计数。但是在调试列表数组时,在模型的每个字段中显示“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; }
}

3 个解决方案

#1


3  

All the properties on ProfileDetailModel are private. You can check the default visibility for clases, structs, etc... here: https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

ProfileDetailModel上的所有属性都是私有的。您可以检查clases、struct等的默认可见性……在这里:https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Simply add 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; }
}    

#2


2  

You should change all your properties ibn the model class to be public:

您应该将所有属性ibn模型类更改为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; }
}

#3


0  

Add JsonProperty attribute to all the properties. Refer the below screenshot.

向所有属性添加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; }
}

#1


3  

All the properties on ProfileDetailModel are private. You can check the default visibility for clases, structs, etc... here: https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

ProfileDetailModel上的所有属性都是私有的。您可以检查clases、struct等的默认可见性……在这里:https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Simply add 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; }
}    

#2


2  

You should change all your properties ibn the model class to be public:

您应该将所有属性ibn模型类更改为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; }
}

#3


0  

Add JsonProperty attribute to all the properties. Refer the below screenshot.

向所有属性添加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; }
}