c#json使用属性序列化
我喜欢使用RESTAPI将.net连接器写入Eureka注册服务。它要求如下所示的json格式,并希望从配置类生成该json。在类似于“securePort”的json中,找不到如何序列化某些属性,例如看起来像$的属性和@开头的属性:{“$”:“8443”,“@enabled”:“true”} eureka所需的json:c#json使用属性序列化,c#,json,serialization,netflix-eureka,C#,Json,Serialization,Netflix Eureka,我喜欢使用RESTAPI将.net连接器写入Eureka注册服务。它要求如下所示的json格式,并希望从配置类生成该json。在类似于“securePort”的json中,找不到如何序列化某些属性,例如看起来像$的属性和@开头的属性:{“$”:“8443”,“@enabled”:“true”} eureka所需的json: { "instance": { "hostName": "WKS-SOF-L011", "app": "com.automationr
{
"instance": {
"hostName": "WKS-SOF-L011",
"app": "com.automationrhapsody.eureka.app",
"vipAddress": "com.automationrhapsody.eureka.app",
"secureVipAddress": "com.automationrhapsody.eureka.app"
"ipAddr": "10.0.0.10",
"status": "STARTING",
"port": {"$": "8080", "@enabled": "true"},
"securePort": {"$": "8443", "@enabled": "true"},
"healthCheckUrl": "http://WKS-SOF-L011:8080/healthcheck",
"statusPageUrl": "http://WKS-SOF-L011:8080/status",
"homePageUrl": "http://WKS-SOF-L011:8080",
"dataCenterInfo": {
"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo",
"name": "MyOwn"
},
}
}
我的类在序列化后生成下面的json(我使用newtonsoft.json):
您非常接近解决方案。您可以使用
JsonProperty
属性来完成您的工作,就像您已经对其余属性所做的那样
public class Port
{
[JsonProperty("$")]
public string PortName { get; set; }
[JsonProperty("@enabled")]
public bool Enabled { get; set; }
}
在将
[JsonProperty($”)
添加到端口
实体中的端口名
属性后,是否尝试过?
public class Port
{
[JsonProperty("$")]
public string PortName { get; set; }
[JsonProperty("@enabled")]
public bool Enabled { get; set; }
}