本文介绍了System.Text.Json.JsonSerializer.Serialize返回空的Json对象"{}".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
环境:Visual Studio 2019 16.3.8,.NET 3.0.100,.NET Core 3.0单元测试.
Environment: Visual Studio 2019 16.3.8, .NET 3.0.100, .NET Core 3.0 unit test.
下面对System.Text.Json.JsonSerializer.Serialize的所有3个调用都返回空对象:"{}"
All 3 calls below to System.Text.Json.JsonSerializer.Serialize return empty objects: "{}"
我一定做错了什么...但是我看不到吗?
I must be doing something wrong ... but I just don't see it?
public class MyObj
{
public int myInt;
}
[TestMethod]
public void SerializeTest()
{
var myObj = new MyObj() { myInt = 99 };
var txt1 = System.Text.Json.JsonSerializer.Serialize(myObj);
var txt2 = System.Text.Json.JsonSerializer.Serialize(myObj, typeof(MyObj));
var txt3 = System.Text.Json.JsonSerializer.Serialize<MyObj>(myObj);
}
推荐答案
我非常确定序列化程序不适用于字段.因此请改用属性.
im pretty sure the serializer doesn't work with fields.so use a property instead.
public int MyInt { get; set; }
这篇关于System.Text.Json.JsonSerializer.Serialize返回空的Json对象"{}".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!