Newtonsoft.JSON 序列化对象返回空的 JSON 字符串 [英] Newtonsoft.JSON serializeobject returns empty JSON string
问题描述
每个人都经常遇到这个问题,我几乎尝试了所有方法,但没有一个对我有用.
Everyone got this question a lot, and I tried almost everything but none of it works for me.
所以我正在 Xamarin.Forms 中进行开发,并将我的数据发送到服务器.我有这门课:
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; }
}
然后我使用了 Newtonsoft 的 SerializeObject 方法:
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);
但是变量 item
会生成一个包含空 JSON 对象 {}
的字符串.我的实现有问题吗?
But variable item
results in a string containing an empty JSON object {}
. Is something wrong with my implementation?
另外,我注意到虽然我的 Customer 类及其成员是公共的,但调试器仍将它们视为非公共"成员.请参阅调试器截图":
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":
推荐答案
Xamarin Live Player 的序列化程序似乎存在一些问题.我尝试通过 USB 插入手机,它可以工作!
It appears that Xamarin Live Player has some issues with the serializer. I tried plugging my phone via USB and it works!
这篇关于Newtonsoft.JSON 序列化对象返回空的 JSON 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!