I'm getting JSON from an API, each domain like Google, and Facebook will be dynamic. I'm struggling to access the JSON to then put on a webpage, normally APIs are no issue, but the fact its dynamic is causing problems. Furthermore, no solution on StackOverflow is solving my problem. I've already tried this solution, but unfortunately, it didn't work.
So an example response
{
"data": {
"google.com": {
"domain_authority": 95,
"page_authority": 88,
"spam_score": 1,
"nofollow_links": 76221395,
"dofollow_links": 465226564
},
"facebook.com": {
"domain_authority": 96,
"page_authority": 100,
"spam_score": 1,
"nofollow_links": 97570534,
"dofollow_links": 565869181
},
"wikipedia.org": {
"domain_authority": 90,
"page_authority": 75,
"spam_score": 1,
"nofollow_links": 1897582,
"dofollow_links": 20437023
}
}
}
My code to get the value of Google from the example response:
IRestResponse response = client.Execute(request);
// response.Content returns the JSON
var w = new JavaScriptSerializer().Deserialize<Rootobject>(response.Content);
//trying to echo out Google's Domain Authority.
Response.Write(w.data[0].domain_authority);
public class Rootobject
{
public Data data { get; set; }
}
public class Data
{
public int domain_authority { get; set; }
public int page_authority { get; set; }
public int spam_score { get; set; }
public int nofollow_links { get; set; }
public int dofollow_links { get; set; }
}
Latest attempt (working although I cannot get the domains names via JSON):
IRestResponse response = client.Execute(request);
var root = JsonConvert.DeserializeObject<Rootobject>(response.Content);
var json2 = JsonConvert.SerializeObject(root, Newtonsoft.Json.Formatting.Indented);
var list = root.data.Values;
int c = 1;
foreach (var domains in list)
{
Response.Write(" "+c+":" + domains.domain_authority);
c++;
}
public class Rootobject
{
public Dictionary<string, Data> data { get; set; }
}
public class Data
{
public int domain_authority { get; set; }
public int page_authority { get; set; }
public int spam_score { get; set; }
public int nofollow_links { get; set; }
public int dofollow_links { get; set; }
}
Neither of the following work -- and I have a feeling I'm being silly (relatively new to C#, so sorry if it's obvious).