简体   繁体   中英

Converting JSON string array into c# List object returning “null” value for each field of a Model inside a List

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

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.

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

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

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

You should change all your properties ibn the model class to be 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; }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM