我正在尝试用C#创建一个自定义类型,它可以rdap.org在JSON反序列化时处理www.example.com响应(出于我的目的,我将使用netwonsoft)。
例如,在向以下对象发出GET请求时:https://rdap.org/domain/stackoverflow.com返回以下JSON字符串:
{
"objectClassName": "domain",
"handle": "108907621_DOMAIN_COM-VRSN",
"ldhName": "STACKOVERFLOW.COM",
"links": [
{
"value": "https://rdap.verisign.com/com/v1/domain/STACKOVERFLOW.COM",
"rel": "self",
"href": "https://rdap.verisign.com/com/v1/domain/STACKOVERFLOW.COM",
"type": "application/rdap+json"
},
{
"value": "https://apis.cscglobal.com/dbs/rdap-api/v1/domain/STACKOVERFLOW.COM",
"rel": "related",
"href": "https://apis.cscglobal.com/dbs/rdap-api/v1/domain/STACKOVERFLOW.COM",
"type": "application/rdap+json"
}
],
"status": [
"client transfer prohibited"
],
"entities": [
{
"objectClassName": "entity",
"handle": "299",
"roles": [
"registrar"
],
"publicIds": [
{
"type": "IANA Registrar ID",
"identifier": "299"
}
],
"vcardArray": [
"vcard",
[
[
"version",
{},
"text",
"4.0"
],
[
"fn",
{},
"text",
"CSC Corporate Domains, Inc."
]
]
],
"entities": [
{
"objectClassName": "entity",
"roles": [
"abuse"
],
"vcardArray": [
"vcard",
[
[
"version",
{},
"text",
"4.0"
],
[
"fn",
{},
"text",
""
],
[
"tel",
{
"type": "voice"
},
"uri",
"tel:8887802723"
],
[
"email",
{},
"text",
"domainabuse@cscglobal.com"
]
]
]
}
]
}
],
"events": [
{
"eventAction": "registration",
"eventDate": "2003-12-26T19:18:07Z"
},
{
"eventAction": "expiration",
"eventDate": "2024-02-02T11:59:59Z"
},
{
"eventAction": "last changed",
"eventDate": "2022-08-17T04:32:10Z"
},
{
"eventAction": "last update of RDAP database",
"eventDate": "2023-01-29T11:05:33Z"
}
],
"secureDNS": {
"delegationSigned": false
},
"nameservers": [
{
"objectClassName": "nameserver",
"ldhName": "NS-1033.AWSDNS-01.ORG"
},
{
"objectClassName": "nameserver",
"ldhName": "NS-358.AWSDNS-44.COM"
},
{
"objectClassName": "nameserver",
"ldhName": "NS-CLOUD-E1.GOOGLEDOMAINS.COM"
},
{
"objectClassName": "nameserver",
"ldhName": "NS-CLOUD-E2.GOOGLEDOMAINS.COM"
}
],
"rdapConformance": [
"rdap_level_0",
"icann_rdap_technical_implementation_guide_0",
"icann_rdap_response_profile_0"
],
"notices": [
{
"title": "Terms of Use",
"description": [
"Service subject to Terms of Use."
],
"links": [
{
"href": "https://www.verisign.com/domain-names/registration-data-access-protocol/terms-service/index.xhtml",
"type": "text/html"
}
]
},
{
"title": "Status Codes",
"description": [
"For more information on domain status codes, please visit https://icann.org/epp"
],
"links": [
{
"href": "https://icann.org/epp",
"type": "text/html"
}
]
},
{
"title": "RDDS Inaccuracy Complaint Form",
"description": [
"URL of the ICANN RDDS Inaccuracy Complaint Form: https://icann.org/wicf"
],
"links": [
{
"href": "https://icann.org/wicf",
"type": "text/html"
}
]
}
]
}
根据json2csharp.com,以下自定义类型将执行此操作:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Entity
{
public string objectClassName { get; set; }
public List<string> roles { get; set; }
public List<Remark> remarks { get; set; }
public List<string> status { get; set; }
public string handle { get; set; }
public List<object> vcardArray { get; set; }
public List<Entity> entities { get; set; }
public List<PublicId> publicIds { get; set; }
public List<Event> events { get; set; }
public List<Link> links { get; set; }
}
public class Event
{
public string eventAction { get; set; }
public DateTime eventDate { get; set; }
public string eventActor { get; set; }
}
public class Link
{
public string rel { get; set; }
public string value { get; set; }
public string href { get; set; }
public string type { get; set; }
public string title { get; set; }
}
public class Nameserver
{
public string objectClassName { get; set; }
public string handle { get; set; }
public string ldhName { get; set; }
public List<string> status { get; set; }
public List<Event> events { get; set; }
public List<Link> links { get; set; }
}
public class Notice
{
public string title { get; set; }
public List<string> description { get; set; }
public List<Link> links { get; set; }
}
public class PublicId
{
public string identifier { get; set; }
public string type { get; set; }
}
public class Remark
{
public string title { get; set; }
public string type { get; set; }
public List<string> description { get; set; }
public List<Link> links { get; set; }
}
public class Root
{
public List<string> rdapConformance { get; set; }
public string objectClassName { get; set; }
public string handle { get; set; }
public string ldhName { get; set; }
public List<string> status { get; set; }
public List<Event> events { get; set; }
public SecureDNS secureDNS { get; set; }
public List<Entity> entities { get; set; }
public List<Nameserver> nameservers { get; set; }
public List<Notice> notices { get; set; }
public List<Link> links { get; set; }
}
public class SecureDNS
{
public bool delegationSigned { get; set; }
}
事实上,它确实正确地将json反序列化为类型,只是有一个非常小而微妙的问题:源代码public List<object> vcardArray { get; set; }
中的一行使用了一个“object”列表,使得访问vcardArray非常困难。我目前正在将每个对象转换为字符串,并使用正则表达式访问它们,但这似乎不正确,感觉很挑剔。
有没有什么方法可以修改Entity类而不使用vcardArray的“object”?也许可以使用其他类?到目前为止,我所做的每一次尝试都在反序列化时失败。
1条答案
按热度按时间eqfvzcg81#
可以创建VCard类并将JsonConstructor添加到Entity类