How do I get all the nodes with a certain name

Viewed 399

So I'm trying to parse some json data.. I want to get all the "id"'s from each "owner" node in a list of strings but I can't seem to access the nodes by doing this.

string theData = File.ReadAllText("data.txt");
dynamic json = JObject.Parse(theData);

Here is what the data looks like https://hatebin.com/tqfvnp5ndgyn

How do I properly get all the "id"'s from each "owner" node and put them in a new List<string>?

2 Answers

See if your particular JSON object lends itself to LINQ queries.

For example:

https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm

var postTitles =
    from p in rss["channel"]["item"]
    select (string)p["title"];

... or ...

var categories =
    from c in rss["channel"]["item"].SelectMany(i => i["categories"]).Values<string>()
    group c by c
    into g
    orderby g.Count() descending
    select new { Category = g.Key, Count = g.Count() };

Source JSON used in the above examples:

string json = @"{
  'channel': {
    'title': 'James Newton-King',
    'link': 'http://james.newtonking.com',
    'description': 'James Newton-King\'s blog.',
    'item': [
      {
        'title': 'Json.NET 1.3 + New license + Now on CodePlex',
        'description': 'Announcing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
        'link': 'http://james.newtonking.com/projects/json-net.aspx',
        'categories': [
          'Json.NET',
          'CodePlex'
        ]
      },
      {
        'title': 'LINQ to JSON beta',
        'description': 'Announcing LINQ to JSON',
        'link': 'http://james.newtonking.com/projects/json-net.aspx',
        'categories': [
          'Json.NET',
          'LINQ'
        ]
      }
    ]
  }
}";

First create your models into your project

    public class Config
{
    public string csrf_token { get; set; }
    public object viewer { get; set; }
    public object viewerId { get; set; }
}

public class PageInfo
{
    public bool has_next_page { get; set; }
    public string end_cursor { get; set; }
}

public class Node
{
    public string text { get; set; }
}

public class Edge
{
    public Node node { get; set; }
}

public class EdgeMediaToCaption
{
    public IList<Edge> edges { get; set; }
}

public class EdgeMediaToComment
{
    public int count { get; set; }
}

public class Dimensions
{
    public int height { get; set; }
    public int width { get; set; }
}

public class EdgeLikedBy
{
    public int count { get; set; }
}

public class EdgeMediaPreviewLike
{
    public int count { get; set; }
}

public class Owner
{
    public object id { get; set; }
}

public class ThumbnailResource
{
    public string src { get; set; }
    public int config_width { get; set; }
    public int config_height { get; set; }
}

public class Node
{
    public bool comments_disabled { get; set; }
    public string __typename { get; set; }
    public string id { get; set; }
    public EdgeMediaToCaption edge_media_to_caption { get; set; }
    public string shortcode { get; set; }
    public EdgeMediaToComment edge_media_to_comment { get; set; }
    public int taken_at_timestamp { get; set; }
    public Dimensions dimensions { get; set; }
    public string display_url { get; set; }
    public EdgeLikedBy edge_liked_by { get; set; }
    public EdgeMediaPreviewLike edge_media_preview_like { get; set; }
    public Owner owner { get; set; }
    public string thumbnail_src { get; set; }
    public IList<ThumbnailResource> thumbnail_resources { get; set; }
    public bool is_video { get; set; }
    public string accessibility_caption { get; set; }
    public string product_type { get; set; }
    public int video_view_count { get; set; }
}

public class Edge
{
    public Node node { get; set; }
}

public class EdgeHashtagToMedia
{
    public int count { get; set; }
    public PageInfo page_info { get; set; }
    public IList<Edge> edges { get; set; }
}

public class Hashtag
{
    public string id { get; set; }
    public string name { get; set; }
    public bool allow_following { get; set; }
    public string description { get; set; }
    public bool is_following { get; set; }
    public bool is_top_media_only { get; set; }
    public string profile_pic_url { get; set; }
    public EdgeHashtagToMedia edge_hashtag_to_media { get; set; }
}

public class Graphql
{
    public Hashtag hashtag { get; set; }
}

public class TagPage
{
    public Graphql graphql { get; set; }
}

public class EntryData
{
    public IList<TagPage> TagPage { get; set; }
}

public class Example
{
    public Config config { get; set; }
    public string country_code { get; set; }
    public string language_code { get; set; }
    public string locale { get; set; }
    public EntryData entry_data { get; set; }
}

using Microsoft newtonsoft json converter library here DeserializeObject your objects

now you can handle ids of Owner's easy.

            var nodeIdList = new List<string>();
        Example res = JsonConvert.DeserializeObject<Example>(yourJson);
        res.entry_data.TagPage.ToList().ForEach(
            x => x.graphql.hashtag.edge_hashtag_to_media.edges.ToList().ForEach(y => nodeIdList.Add(y.node.id)));
Related