简体   繁体   English

反序列化返回所有值都设置为null的对象

[英]Deserialize returns object with all values set to null

String str ="{\"_id\":\"eta_emp_1\",\"_rev\":\"446-195e9341df50aeed33d2cb833420b100\",";
str+="\"channels\":[\"ch_pri-eta_org\"],\"doc_type\":\"emp\",\"downloaded\":true,";
str+="\"eta_code\":\"abhi\",\"f_name\":\"Abhilash\",\"isactive\":true,";
str+="\"isadmin\":true,\"l_name\":\"Dhondalkar\",";
str+="\"lat\":17.69967582522918,\"lon\":75.89857880026102,";
str+="\"m_name\":\"Dheeraj\",\"mod_at\":1494693566503,\"mod_by\":\"eta_emp_1\",";
str+="\"org_id\":\"eta_org\",\"pwd\":\"abcde\",";
str+="\"reported_by\":[\"e27f41e7-5b84-4c86-838e-97598b4d20a0\",\"8daa0901-f6e8-4fdd-b41c-658aa901899d\",";
str+="\"9bf72297-481c-4d59-a0f4-d5549cb60e27\",\"2e5ce994-2cfb-4e62-88f8-ff1d3a069ca0\"]}";

UserDetails userDetails = JsonConvert.DeserializeObject<UserDetails>(str);



 public class UserDetails
    {

    String _id{get;set;}
    String _rev { get; set; }
    String[] channels{get;set;} 
    String doc_type{get;set;} 
    bool downloaded{get;set;} 
    String eta_code{get;set;} 
    String f_name{get;set;}
    bool isactive { get; set; }
    bool isadmin { get; set; } 
    long l_name{get;set;}
    long lat{get;set;}
    String lon{get;set;}
    String m_name{get;set;}
    long mod_at{get;set;} 
    String mod_by{get;set;}
    String org_id{get;set;}
    String pwd{get;set;} 
    String[] reported_by{get;set;}
    }

All values are set to null. 所有值均设置为null。 Not able to figure out issue. 无法找出问题。 Pl help PL帮助

This class should work: 该类应该工作:

public class UserDetails
{
        public string _id { get; set; }
        public string _rev { get; set; }
        public List<string> channels { get; set; }
        public string doc_type { get; set; }
        public bool downloaded { get; set; }
        public string eta_code { get; set; }
        public string f_name { get; set; }
        public bool isactive { get; set; }
        public bool isadmin { get; set; }
        public string l_name { get; set; }
        public double lat { get; set; }
        public double lon { get; set; }
        public string m_name { get; set; }
        public long mod_at { get; set; }
        public string mod_by { get; set; }
        public string org_id { get; set; }
        public string pwd { get; set; }
        public List<string> reported_by { get; set; }
}

More help: 更多帮助:

http://json2csharp.com http://json2csharp.com

Based on my previous experience with JSON.NET, it can be a bit tricky the translation from JSON to the right class. 根据我以前对JSON.NET的经验,将JSON转换为正确的类可能会有些棘手。 The web site above have provided me all the time the right class. 上面的网站一直为我提供合适的课程。 The only exception is the Wikipedia API because it's a dynamic API and you need to do a small trick. 唯一的例外是Wikipedia API,因为它是动态API,您需要做一些小技巧。

Also, keep in mind all properties must be public because if something is private (like in your question), it is not going to be accessible by the translator or anything else. 另外,请记住,所有属性都必须是公共的,因为如果某些内容是私有的(例如您的问题),那么翻译者或其他任何人都无法访问它。 I hope it's helpful. 希望对您有所帮助。

Either mark all the properties in your class as public, or explicity add JsonProperty attribute to each property as follows: 可以将类中的所有属性标记为public,或者将JsonProperty属性显式添加到每个属性,如下所示:

public class UserDetails
    {
        [JsonProperty("_id")]
        String _id { get; set; }

        ...
    }

When using Json Validator I got "The root JSON data must represent an object or an array", therefore indicating that your JSON is most likey not correct. 当使用Json Validator时,我得到“根JSON数据必须代表一个对象或数组”,因此表明您的JSON最有可能是不正确的。 This is why the string cannot be serialized to your class, leaving the properties null . 这就是为什么不能将string序列化到您的类,而将属性保留为null You should show us your serialization process to find out about where the error is. 您应该向我们展示您的序列化过程,以找出错误所在。

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

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