Newtonsoft.JSON serializeobject returns an empty JSON string

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

So, I am developing in Xamarin.Forms and am going 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 the Newtonsoft 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 the item variable results in a string containing an empty JSON {} object. Is there something wrong with my implementation?

EDIT: Also, I noticed that although my Customer class and its members are publicly available, the debugger still considers them to be โ€œnon-publicโ€ members. See "Screenshot of the debugger":

Screenshot with debugger

+5
source share
2 answers

It seems that Xamarin Live Player has some serializer issues. I tried to connect my phone via USB and it works!

+3
source

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

In addition, I noticed that although my Customer class is public, its members are not public:

Given that these are your options:

  • If your properties may be publicly available, make them publicly available.
  • If properties should remain private, add the [JsonProperty] attribute to them
+3
source

Source: https://habr.com/ru/post/1275487/


All Articles