Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

c# - Deserialized Object Has All Values Set to Null

I'm trying to deserialize JSON into a custom object but all my properties are set to null and not sure what's going on. Does anyone see anything wrong?

JSON Example

{
"Keys": [
    {
        "RegistrationKey": "asdfasdfa",
        "ValidationStatus": "Valid",
        "ValidationDescription": null,
        "Properties": [
            {
                "Key": "Guid",
                "Value": "i0asd23165323sdfs68661358"
            }
        ]
    }
 ]
}

Here is my Code, where strResponseValid is the JSON above.

Keys myDeserializedObjValid = (Keys)JsonConvert.DeserializeObject(strResponseValid, typeof(Keys));
validationStatusValid = myDeserializedObjValid.ValidationStatus;

Here are my classes

    public class Keys
    {
        public string RegistrationKey { get; set; }
        public string ValidationStatus { get; set; }
        public string ValidationDescription { get; set; }
        public List<Properties> PropertiesList { get; set; }
    }

    public class Properties
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

in my case it was because of my destination type have internal (or private) set modifiers for those properties .

public class Summary{

     public Class2 Prop1 { get; internal set; }
     public Class1 prop2 { get; set; }

}

after removing internal modifier and making them implicitly public, Json.net deserialize those objects too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...