简体   繁体   English

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

[英]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. 我有字符串格式的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. 我在下面的代码行中使用,但是它为Model的每个字段返回值。它在“ objectList.Count”行中显示正确的计数,但是在调试列表数组时,它对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. ProfileDetailModel上的所有属性都是私有的。 You can check the default visibility for clases, structs, etc... here: https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx 您可以在此处检查小节,结构等的默认可见性: https : //msdn.microsoft.com/zh-cn/library/ba0a1yw2.aspx

Simply add public 只需添加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

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. 将JsonProperty属性添加到所有属性。 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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