Newtonsoft.JSON serializeobject returns empty JSON string [duplicate]

♀尐吖头ヾ 提交于 2019-12-19 19:57:34

问题


Everyone got this question a lot, and I tried almost everything but none of it works for me.

So I am developing in Xamarin.Forms and is about to send my data to the server. I have this class:

public class Customer
{

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string BirthDate { get; set; }
    public string Password { get; set; }
    public string EmailAddress { get; set; }
    public string ContactNumber { get; set; }

}

Then I used Newtonsoft's SerializeObject method:

Customer customer = new Customer
        {
            FirstName = FirstName.Text,
            LastName = LastName.Text,
            BirthDate = BirthDate.Date.ToString(),
            EmailAddress = Email.Text,
            Password = Password.Text,
            ContactNumber = Mobile.Text
        };


        var item = JsonConvert.SerializeObject(customer);

But variable item results in a string containing an empty JSON object {}. Is something wrong with my implementation?

EDIT: Also, I noticed that although my Customer class and its members are public, the debugger still counts them as "non-public" members. Please see "screenshot of debugger":


回答1:


Your question is a little misleading since you show your class properties as public.

Also, I noticed that although my Customer class is public, its members are non-public:

WIth that being said these are your options:

  1. If your properties can be public, make them public
  2. If the properties need to remain private, add the [JsonProperty] attribute to them



回答2:


It appears that Xamarin Live Player has some issues with the serializer. I tried plugging my phone via USB and it works!



来源:https://stackoverflow.com/questions/48914561/newtonsoft-json-serializeobject-returns-empty-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!