I'm struggling to represent this JSON as C# class structures (snipped for brevity):
{
"subdomain_count": 1118434,
"hostname": "google.com",
"endpoint": "/v1/domain/google.com",
"current_dns": {
"txt": {
"values": [
{
"value": "v=spf1 include:_spf.google.com ~all"
},
{
"value": "MS=E4A68B9AB2BB9670BCE15412F62916164C0B20BB"
}
],
"first_seen": "2021-04-22"
},
},
"apex_domain": "google.com",
"alexa_rank": 1
}
I'm looking for help on how to represent this deeply-nested portion:
"current_dns": {
"txt": {
"values": [
{
"value": "v=spf1 include:_spf.google.com ~all"
},
I've tried various combinations like List<List<string>>
and Dictionary<Dictionary<List<string>, string>, string>
without any luck.
CodePudding user response:
It's not uniform. You have
.current_dns.txt.values
is an array.current_dns.txt.first_seen
is a string.
So no generic dictionary/list combination will do.
{ "txt": ... }
is a JSON object. Those normally represent one of two things:- A dictionary (e.g.
Dictionary
) - A struct (e.g.
struct
,class
)
Without additional information about the JSON format, some guess work is needed here. Since
TXT
is one of many DNS record types, I presumecurrent_dns
is a collection of info grouped by DNS record type. As such, I think aDictionary
would work best here, but there's room for interpretation and preference.- A dictionary (e.g.
{ "values": [ ... ], "first_seen": "2021-04-22" }
is also a JSON object. But this is clearly not a dictionary since the values have different types. (It's "not uniform".) We're going to have to build astruct
orclass
.[ ... ]
is a JSON array.List
is the go-to choice for those.{ "value": "v=spf1 include:_spf.google.com ~all" }
is yet another JSON object. It looks like it always has the singular keyvalue
, so we'll use a class for it.
This gets us the following:
public class DnsResultValue {
public string value { get; set; }
}
public class DnsResult {
public List<DnsResultValue> values { get; set; }
public string first_seen { get; set; }
}
public Dictionary<string, DnsResult> current_dns { get; set; }
CodePudding user response:
Dictionary<string, Dictionary<string, Dictionary<string, string>[]>>