Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#json使用属性序列化_C#_Json_Serialization_Netflix Eureka - Fatal编程技术网

c#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

我喜欢使用RESTAPI将.net连接器写入Eureka注册服务。它要求如下所示的json格式,并希望从配置类生成该json。在类似于“securePort”的json中,找不到如何序列化某些属性,例如看起来像$的属性和@开头的属性:{“$”:“8443”,“@enabled”:“true”}

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"
        },
    }
}
我的类在序列化后生成下面的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; }
}