json对元祖反序列化问题

2019-11-05 16:13发布

对元祖反序列化后结果不符合预期,请问应该怎么设置才正常

 [Fact]
        public void JsonTest()
        {
            var input = new A()
            {
                MyProperty = new Tuple<string, string>("1", "1"),
                MyProperty2 = "1"
            };
            var jsonStr = JsonConvert.SerializeObject(input);
            Log.Information(jsonStr);

            input = JsonConvert.DeserializeObject<A>(jsonStr);
            jsonStr = JsonConvert.SerializeObject(input);
            Log.Information(jsonStr);
        }

        class A
        {
            public A()
            {
                MyProperty = new Tuple<string, string>("2", "2");//把这一行去掉就正常
                MyProperty2 = "3";
            }

            public Tuple<string, string> MyProperty { get; set; } 
            public string MyProperty2 { get; set; } = "2";
        }

输出结果为

[14:58:09 INF] {"MyProperty":{"Item1":"1","Item2":"1"},"MyProperty2":"1"}
[14:58:09 INF] {"MyProperty":{"Item1":"2","Item2":"2"},"MyProperty2":"1"}//期望的结果是和上面一样,但是MyProperty却变了

1条回答
The star\"
2楼-- · 2019-11-05 16:52

因为里面的属性是只读的不能被修改

查看更多
登录 后发表回答