简体   繁体   English

Json正确反序列化密钥但不值

[英]Json deserializing key correctly but not value

I am trying to deserialize a JSON string into object. 我正在尝试将JSON字符串反序列化为对象。 Following is the class of network interface names and a list of IP address mappings: 以下是网络接口名称的类别和IP地址映射的列表:

[DataContract]
public class NicMappings
{
    public NicMappings()
    {
        this.Ips = new List<IPAddress>();
    }

    [DataMember]
    public string NetworkInterfaceName { get; set; }

    [DataMember]
    public List<IPAddress> Ips { get; }
}

There is another class Settings which contains a list of NicMappings: 还有另一个类Settings,它包含NicMappings列表:

[DataContract]
public class Settings
{
    public Settings()
    {
        this.PrivateIps = new List<IPAddress>();
        this.VirtualIps = new List<IPAddress>();
        this.IpRanges = new List<IPPrefix>();
        this.NicMappings = new List<NicMappings>();
    }

    [DataMember]
    public List<IPAddress> PrivateIps { get; private set; }

    [DataMember]
    public List<IPAddress> VirtualIps { get; private set; }

    [DataMember]
    public List<IPPrefix> IpRanges { get; private set; }

    [DataMember]
    public List<NicMappings> NicMappingsList { get; private set; }
}

For testing, I am reading a single NicMapping containing a name and a single IP which gets serialized fine (along with other members of the Settings class). 为了进行测试,我正在读取单个NicMapping,其中包含一个名称和一个可以序列化的IP(以及Settings类的其他成员)。 This is the output json: 这是输出json:

{"PrivateIps":["10.0.0.1","10.0.0.2","10.0.0.3"],..."NicMappings":[{"NetworkInterfaceName":"Nic","Ips":["127.0.0.1"]}]}

Deserialization is the problem. 反序列化是问题所在。 All the other settings get deserialized fine, except for NicMappings. 除NicMappings之外,其他所有设置都可以反序列化。 It deserializes the name fine, but doesn't deserialize the Ips. 它可以反序列化名称,但不会反序列化Ips。 It doesn't even throw an exception. 它甚至不会引发异常。 I added a log to statement to check the count of Ips, it prints 0. This is the code I'm using to deserialize it: 我在语句中添加了一条日志以检查Ips的计数,它显示0。这是我用来反序列化的代码:

Settings settings = Serializer.GetSettings<Settings>(file);

Serializer is a class which has JsonSerializerSettings object with a binder and some converters defined. 序列化器是一个类,具有JsonSerializerSettings对象,该对象具有活页夹和一些定义的转换器。

public T GetSettings(string file)
{
    string json = File.ReadAllText(file);
    return Deserialize<T>(json);
}

Next, 下一个,

List<NicData> nicData = GetNicData(settings.NicMappingsList);

private List<NicData> GetNicData(List<NicMappings> NicMappingsList)
{
    List<NicData> NicDatas = new List<NicData>();
    foreach (NicMappings nicMapping in NicMappingsList)
    {
        NicData nicData = new NicData
        {
            NetworkInterfaceName = nicMapping.NetworkInterfaceName
        };

        foreach (IPAddress ipAddress in nicMapping.Ips)
        {
            nicData.Ips.Add(ipAddress.ToString());
        }

        nicDataList.Add(NicData);
    }

    return nicDataList;
}

The above returns me a list with a single element, with the correct name, but an empty Ips list. 上面的代码为我返回了一个列表,其中包含一个具有正确名称的单个元素,但包含一个空的Ips列表。

Btw, NicData is the pretty much the same as NicMappings 顺便说一句,NicData与NicMappings几乎相同

public class NicData
{
    public NicData()
    {
        Ips = new List<string>();
    }

    public string NetworkInterfaceName { get; set; }

    public List<string> Ips { get; }
}

I'm not sure where to look. 我不确定要看哪里。 Any idea which part of the code should I check for problem? 知道应该检查代码的哪一部分吗? Please let me know if the problem is not clear or you need more info. 如果问题仍未解决,或者您需要更多信息,请告诉我。 Thanks! 谢谢!

Try to add a setter: 尝试添加二传手:

public List<string> Ips { get; set;}

More info on serialization here 有关序列化的更多信息,请点击此处

It says: 它说:

DO implement a getter and setter on all properties that have Data-MemberAttribute. 不要在具有Data-MemberAttribute的所有属性上实现getter和setter。 Data contract serializers require both the getter and the setter for the type to be considered serializable. 数据协定序列化程序要求将类型的getter和setter都视为可序列化的。 If the type won't be used in partial trust, one or both of the property accessors can be nonpublic. 如果将不以部分信任的方式使用该类型,则属性访问器中的一个或两个可以是非公共的。

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

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