Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取具有特定名称的所有节点_C#_.net_Json_Json.net - Fatal编程技术网

C# 如何获取具有特定名称的所有节点

C# 如何获取具有特定名称的所有节点,c#,.net,json,json.net,C#,.net,Json,Json.net,所以我试图解析一些json数据。。我想从字符串列表中的每个“所有者”节点获取所有“id”,但这样做似乎无法访问节点 string theData = File.ReadAllText("data.txt"); dynamic json = JObject.Parse(theData); 下面是数据的样子 如何正确地从每个“所有者”节点获取所有“id”,并将它们放入新列表中?查看您的特定JSON对象是否适合LINQ查询 例如: 。。。或者 var categories = from

所以我试图解析一些json数据。。我想从字符串列表中的每个
“所有者”
节点获取所有
“id”
,但这样做似乎无法访问节点

string theData = File.ReadAllText("data.txt");
dynamic json = JObject.Parse(theData);
下面是数据的样子


如何正确地从每个
“所有者”
节点获取所有
“id”
,并将它们放入
新列表中?

查看您的特定JSON对象是否适合LINQ查询

例如:

。。。或者

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

首先在项目中创建模型

    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; }
}
公共类配置
{
公共字符串csrf_标记{get;set;}
公共对象查看器{get;set;}
公共对象viewerId{get;set;}
}
公共类页面信息
{
公共bool有下一页{get;set;}
公共字符串结束\u游标{get;set;}
}
公共类节点
{
公共字符串文本{get;set;}
}
公共阶级边缘
{
公共节点节点{get;set;}
}
公共类边缘定位
{
公共IList边{get;set;}
}
公共类边缘提示
{
公共整数计数{get;set;}
}
公共类维度
{
公共整数高度{get;set;}
公共整数宽度{get;set;}
}
公共类EdgeLikedBy
{
公共整数计数{get;set;}
}
公共类EdgeMediaPreviewLike
{
公共整数计数{get;set;}
}
公共类所有者
{
公共对象id{get;set;}
}
公共类缩略图资源
{
公共字符串src{get;set;}
公共int-config_-width{get;set;}
公共int-config_-height{get;set;}
}
公共类节点
{
公共bool注释\u禁用{get;set;}
公共字符串uuTypeName{get;set;}
公共字符串id{get;set;}
公共边缘媒体选项边缘媒体到标题{get;set;}
公共字符串短码{get;set;}
公共边缘媒体注释边缘媒体到注释{get;set;}
在时间戳{get;set;}处获取公共整数
公共维度维度{get;set;}
公共字符串显示\u url{get;set;}
公共边由{get;set;}的边生成
公共EdgeMediaPreviewLike edge_media_preview_like{get;set;}
公共所有者{get;set;}
公共字符串缩略图\u src{get;set;}
公共IList缩略图_资源{get;set;}
公共bool是_video{get;set;}
公共字符串可访问性_标题{get;set;}
公共字符串乘积_类型{get;set;}
public int video_view_count{get;set;}
}
公共阶级边缘
{
公共节点节点{get;set;}
}
公共类边缘标记媒体
{
公共整数计数{get;set;}
公共页面信息页面_info{get;set;}
公共IList边{get;set;}
}
公共类标签
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共bool allow_跟随{get;set;}
公共字符串说明{get;set;}
公共布尔在{get;set;}之后
公共布尔值仅为{get;set;}
公共字符串配置文件\u pic\u url{get;set;}
公共EdgeHashtagToMedia edge_hashtag_to_media{get;set;}
}
公共类Graphql
{
公共Hashtag Hashtag{get;set;}
}
公共类标记页
{
公共Graphql Graphql{get;set;}
}
公共类入口数据
{
公共IList标记页{get;set;}
}
公开课范例
{
公共配置{get;set;}
公共字符串国家代码{get;set;}
公共字符串语言_代码{get;set;}
公共字符串区域设置{get;set;}
公共入口数据项\u数据{get;set;}
}
使用Microsoft newtonsoft json转换器库反序列化对象对象

现在您可以轻松处理所有者的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)));
var nodeIdList=newlist();
示例res=JsonConvert.DeserializeObject(yourJson);
res.entry_data.TagPage.ToList().ForEach(
x=>x.graphql.hashtag.edge_hashtag_to_media.edges.ToList().ForEach(y=>nodeIdList.Add(y.node.id));
    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; }
}
            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)));