Ti faccio un esempio simile al tuo, penso sia ciò che intendi.
Ho semplificato un pò il JSON per questioni di tempo (così non puoi nemmeno fare proprio copia-incolla).
Le classi per rappresentare il tutto sono:
che è di fatto il contenitore di tutto.
All'interno di Results ci sono due List<> (organic e people also ask):
Quelle classi sono:
Codice completo:
Se vuoi solo alcuni attributi puoi anche fare così:
Qui puoi provarlo anche online: https://dotnetfiddle.net/5I8rOx
Ho semplificato un pò il JSON per questioni di tempo (così non puoi nemmeno fare proprio copia-incolla).
Codice:
string json = @"
{
""results"": {
""organic"": [
{
""cache_url"": null,
""title"": ""example"",
""snippet"": ""17 hours ago"",
""rank"": 1,
""url"": ""example.com"",
""snippet_html"": ""<span>29 mins ago</span>"",
""rich_snippet"": null
},
{
""cache_url"": null,
""title"": ""Example"",
""snippet"": ""29 mins ago"",
""rank"": 2,
""url"":""example.com"",
""snippet_html"": ""<span>29 mins ago</span>"",
""rich_snippet"": null
}
],
""people_also_ask"": [
{
""question"": ""null"",
""source"": ""https://www.example.com""
},
{
""question"": ""null"",
""source"": ""https://www.example.org""
}
]
}
}";
Le classi per rappresentare il tutto sono:
C#:
class RootObject
{
[JsonProperty("results")]
public Results Results { get; set; }
}
che è di fatto il contenitore di tutto.
All'interno di Results ci sono due List<> (organic e people also ask):
C#:
class Results
{
[JsonProperty("organic")]
public List<Organic> Organics { get; set; }
[JsonProperty("people_also_ask")]
public List<PeopleAsk> PeopleAsk { get; set; }
}
Quelle classi sono:
C#:
class Organic
{
[JsonProperty("cache_url")]
public string CacheUrl { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("snippet")]
public string Snippet { get; set; }
[JsonProperty("rank")]
public int Rank { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("snippet_html")]
public string SnippetHtml { get; set; }
[JsonProperty("rich_snippet")]
public string URichSnippet { get; set; }
}
class PeopleAsk
{
[JsonProperty("question")]
public string Question { get; set; }
[JsonProperty("source")]
public string Source { get; set; }
}
Codice completo:
C#:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Example
{
public static void Main()
{
string json = @"
{
""results"": {
""organic"": [
{
""cache_url"": null,
""title"": ""example"",
""snippet"": ""17 hours ago"",
""rank"": 1,
""url"": ""example.com"",
""snippet_html"": ""<span>29 mins ago</span>"",
""rich_snippet"": null
},
{
""cache_url"": null,
""title"": ""Example"",
""snippet"": ""29 mins ago"",
""rank"": 2,
""url"":""example.com"",
""snippet_html"": ""<span>29 mins ago</span>"",
""rich_snippet"": null
}
],
""people_also_ask"": [
{
""question"": ""null"",
""source"": ""https://www.example.com""
},
{
""question"": ""null"",
""source"": ""https://www.example.org""
}
]
}
}";
RootObject obj1 = JsonConvert.DeserializeObject<RootObject>(json);
foreach (Organic organic in obj1.Results.Organics)
{
Console.WriteLine("URL: " + organic.Url);
Console.WriteLine("Title: " + organic.Title);
Console.WriteLine();
}
RootObject obj2 = JsonConvert.DeserializeObject<RootObject>(json);
List<PeopleAsk> PeopleAsk = obj2.Results.PeopleAsk;
foreach (PeopleAsk people in PeopleAsk)
{
Console.WriteLine("URL: " + people.Question);
Console.WriteLine("Title: " + people.Source);
Console.WriteLine();
}
}
class RootObject
{
[JsonProperty("results")]
public Results Results { get; set; }
}
class Results
{
[JsonProperty("organic")]
public List<Organic> Organics { get; set; }
[JsonProperty("people_also_ask")]
public List<PeopleAsk> PeopleAsk { get; set; }
}
class Organic
{
[JsonProperty("cache_url")]
public string CacheUrl { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("snippet")]
public string Snippet { get; set; }
[JsonProperty("rank")]
public int Rank { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("snippet_html")]
public string SnippetHtml { get; set; }
[JsonProperty("rich_snippet")]
public string URichSnippet { get; set; }
}
class PeopleAsk
{
[JsonProperty("question")]
public string Question { get; set; }
[JsonProperty("source")]
public string Source { get; set; }
}
}
Se vuoi solo alcuni attributi puoi anche fare così:
C#:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Example
{
public static void Main()
{
string json = @"
{
""results"": {
""organic"": [
{
""cache_url"": null,
""title"": ""example"",
""snippet"": ""17 hours ago"",
""rank"": 1,
""url"": ""example.com"",
""snippet_html"": ""<span>29 mins ago</span>"",
""rich_snippet"": null
},
{
""cache_url"": null,
""title"": ""Example"",
""snippet"": ""29 mins ago"",
""rank"": 2,
""url"":""example.com"",
""snippet_html"": ""<span>29 mins ago</span>"",
""rich_snippet"": null
}
],
""people_also_ask"": [
{
""question"": ""null"",
""source"": ""https://www.example.com""
},
{
""question"": ""null"",
""source"": ""https://www.example.org""
}
]
}
}";
RootObject obj1 = JsonConvert.DeserializeObject<RootObject>(json);
foreach (Organic organic in obj1.Results.Organics)
{
Console.WriteLine("URL: " + organic.Url);
Console.WriteLine("Title: " + organic.Title);
Console.WriteLine();
}
}
class RootObject
{
[JsonProperty("results")]
public Results Results { get; set; }
}
class Results
{
[JsonProperty("organic")]
public List<Organic> Organics { get; set; }
}
class Organic
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
}
}
Qui puoi provarlo anche online: https://dotnetfiddle.net/5I8rOx