Json.net how to serialize object as value

前端 未结 6 1431
滥情空心
滥情空心 2020-12-15 19:06

I\'ve pored through the docs, StackOverflow, etc., can\'t seem to find this...

What I want to do is serialize/deserialize a simple value-type of object as a value, n

6条回答
  •  爱一瞬间的悲伤
    2020-12-15 19:12

    public class IPAddress
    {
        byte[] bytes;
    
        public override string ToString() {... etc.
    }
    
    IPAddress ip = new IPAddress("192.168.1.2");
    var obj = new () {ipValue = ip.ToString()};
    string json = JsonConverter.SerializeObject(obj);
    

    You are serializing the whole IP address instance. Maybe just try to serialize the address as a string. (This presumes that you have implemented the ToString-method.)

提交回复
热议问题