简体   繁体   English

使用JsonConvert序列化对象时没有结果

[英]Getting no result when Serializing a Object using JsonConvert

This is my class : 这是我的课:

class teacher_details_uploadable
    {
         string firstName;
         string lastName;
         string contactNumber;
         string emailaddress;
        List<string> classes_names = new List<string>();
        List<all_class_details> all_the_classes_under_teacher = new List<all_class_details>();
        public teacher_details_uploadable()
        {
            firstName = Teacher.FirstName;
            lastName = Teacher.Lastname;
            contactNumber = Teacher.Contactnumber;
            emailaddress = Teacher.Emailaddress;
            classes_names = Teacher.Classes_Names;
            all_the_classes_under_teacher = Teacher.All_the_classes_under_teacher;
        }
    }

Code for converting its object into text: 用于将其对象转换为文本的代码:

teacher_details_uploadable teacher = new teacher_details_uploadable();
            var text=JsonConvert.SerializeObject(teacher);

But in text i get this {} 但在文字中我得到这个{}

Json.NET's default behavior only processes fields that are public. Json.NET的默认行为仅处理公共字段。 So change your non-public fields like so: 因此,如下更改您的非公开字段:

 public string firstName;
 public string lastName;
 public string contactNumber;
 public string emailaddress;
 ...

Another work around, if you're unable publicize the fields, is to use the JsonProperty attribute of Json.Net as shown below: 如果您无法公开这些字段,另一种解决方法是使用Json.Net的JsonProperty属性,如下所示:

class teacher_details_uploadable
    {    [JsonProperty]
         string firstName;
         [JsonProperty]
         string lastName;
         [JsonProperty]
         string contactNumber;
         [JsonProperty]
         string emailaddress;
         ...

You're getting a blank text json object because none of your properties are public. 因为没有任何属性是公共的,所以您得到了一个空白文本json对象。 By default they are internal, and will not be serialized. 默认情况下,它们是内部的,并且不会被序列化。

Properties and fields need to be public for JsonConvert to be able to serialize them. 属性和字段必须是公共的,JsonConvert才能对其进行序列化。 Ie string firstName; string firstName; should be public string firstName; 应该是public string firstName; . If you cannot make these fields public consider using a custom Converter . 如果您无法公开这些字段,请考虑使用自定义Converter

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

相关问题 JsonConvert.SerializeObject:序列化空值时出现意外结果 - JsonConvert.SerializeObject: Unexpected result when Serializing null value 使用 JsonConvert 将异常序列化为 Json 时排除 StackTrace - Exclude StackTrace when serializing Exception to Json using JsonConvert 序列化 JSON 对象时结果为“/” - "/" in result when serializing a JSON object 序列化列表时获取集合文本<string>在字典中使用 C# 中的 JsonConvert</string> - Getting Collection text while serializing List<string> inside a Dictionary using JsonConvert in C# 如何使用JSONConvert将变量Result转换为对象? - How can I convert my variable Result to an object using JSONConvert? 使用JsonConvert.SerializeObject C#时JSON结果中的问题 - Issue In JSON Result when using JsonConvert.SerializeObject C# 使用 JsonConvert 我没有任何结果 - Using JsonConvert i dont have any result 将JSON转换为字符串C#时使用JsonConvert.DeserializeObject出错 - Getting error using JsonConvert.DeserializeObject when converting JSON to string c# 在没有具体对象的接口上使用JsonConvert遇到麻烦 - Trouble using JsonConvert with Interfaces without concrete Object 即使使用Newtonsoft.Json.JsonConvert进行序列化,REST服务也会收到(400)错误的特殊字符请求 - Rest service getting (400) Bad Request for special characters even after serializing with Newtonsoft.Json.JsonConvert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM