簡體   English   中英

如何獲取具有特定名稱的所有節點

[英]How do I get all the nodes with a certain name

所以我試圖解析一些 json 數據。我想從字符串列表中的每個"owner"節點獲取所有"id" ,但我似乎無法通過這樣做訪問節點。

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

這是數據的樣子https://hatebin.com/tqfvnp5ndgyn

如何從每個"owner"節點正確獲取所有"id"並將它們放入new List<string>

查看您的特定 JSON object 是否適合 LINQ 查詢。

例如:

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

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

... 或者...

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() };

上述示例中使用的源 JSON:

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'
        ]
      }
    ]
  }
}";

首先將模型創建到您的項目中

    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; }
}

在此處使用 Microsoft newtonsoft json 轉換器庫 DeserializeObject 你的對象

現在您可以輕松處理所有者的 ID。

            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)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM