简体   繁体   English

c#json使用属性序列化

[英]c# json serialize with attributes

I like to write a .net connector to Eureka register service using rest api. 我喜欢使用rest api将.net连接器编写到Eureka注册服务。 It asks for a json format as below and would like to generate that json from configuration class. 它要求如下的json格式,并希望从配置类生成json。 Could not find out how to serialize some properties like attributes which looks like $, and @ at the beginnnig in the json like "securePort": {"$": "8443", "@enabled": "true"}, 无法找出如何在json的beginnnig处将某些属性(如看起来像$和@的属性)序列化,例如“ securePort”:{“ $”:“ 8443”,“ @enabled”:“ true”},

the json needed for eureka: 尤里卡所需的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"
        },
    }
}

my class thats expected generate the json below after serialization(i use newtonsoft.json): 我的类多数民众赞成在序列化后生成下面的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; }
}

You are very close to the solution. 您非常接近解决方案。 You could use the JsonProperty attribute to do your job as you have already done with the rest of the properties. 您可以使用JsonProperty属性来完成您的工作,就像使用其他属性一样。

public class Port
{
    [JsonProperty("$")]
    public string PortName { get; set; }
    [JsonProperty("@enabled")]
    public bool Enabled { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM