【问题标题】:Converting JSON string array into c# List object returning "null" value for each field of a Model inside a List将 JSON 字符串数组转换为 c# List 对象,为 List 中模型的每个字段返回“null”值
【发布时间】: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


    【解决方案1】:

    ProfileDetailModel 上的所有属性都是私有的。您可以在此处检查类、结构等的默认可见性:https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

    只需添加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】:

      您应该将模型类中的所有属性更改为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】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多