我喜欢使用rest api将.net连接器写入Eureka注册服务。它要求如下所示的json格式,并希望从配置类生成该json。无法找到如何序列化一些属性,如看起来像$的属性,以及在json的beginnnig中的@,如“securePort”:{“$”:“8443”,“@ enabled”:“true”},
尤里卡需要json:{
"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):
public class Port
{
public string PortName { get; set; }
[JsonProperty("enabled")]
public bool Enabled { get; set; }
}
public class DataCenterInfo
{
[JsonProperty("class")]
public string Class { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
public class EurekaRegisterParams
{
[JsonProperty("instanceId")]
public string InstanceId { get; set; }
[JsonProperty("hostName")]
public string HostName { get; set; }
[JsonProperty("app")]
public string App { get; set; }
[JsonProperty("ipAddr")]
public string IpAddr { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("port")]
public Port Port { get; set; }
[JsonProperty("securePort")]
public Port SecurePort { get; set; }
[JsonProperty("countryId")]
public string CountryId { get; set; }
[JsonProperty("dataCenterInfo")]
public DataCenterInfo DataCenterInfo { get; set; }
[JsonProperty("homePageUrl")]
public string HomePageUrl { get; set; }
[JsonProperty("statusPageUrl")]
public string StatusPageUrl { get; set; }
[JsonProperty("healthCheckUrl")]
public string HealthCheckUrl { get; set; }
}
答案 0 :(得分:1)
您非常接近解决方案。您可以使用JsonProperty
属性来完成您的工作,就像您已经完成其余属性一样。
public class Port
{
[JsonProperty("$")]
public string PortName { get; set; }
[JsonProperty("@enabled")]
public bool Enabled { get; set; }
}