【问题标题】:Get text in string between text in said string在所述字符串中的文本之间获取字符串中的文本
【发布时间】:2014-02-25 16:44:41
【问题描述】:

假设我有一个看起来像这样的字符串

{
  "_links": {
    "next": "https://api.twitch.tv/kraken/users/test_user1/follows/channels?direction=DESC&limit=25&offset=25",
    "self": "https://api.twitch.tv/kraken/users/test_user1/follows/channels?direction=DESC&limit=25&offset=0"
  },
  "follows": [
    {
      "created_at": "2013-06-02T09:38:45Z",
      "_links": {
        "self": "https://api.twitch.tv/kraken/users/test_user1/follows/channels/test_channel"
      },
      "channel": {
        "banner": null,
        "_id": 1,
        "url": "http://www.twitch.tv/test_channel",
        "mature": null,
        "teams": [

        ],
        "status": null,
        "logo": null,
        "name": "test_channel",
        "video_banner": null,
        "display_name": "test_channel",
        "created_at": "2007-05-22T10:37:47Z",
        "delay": 0,
        "game": null,
        "_links": {
          "stream_key": "https://api.twitch.tv/kraken/channels/test_channel/stream_key",
          "self": "https://api.twitch.tv/kraken/channels/test_channel",
          "videos": "https://api.twitch.tv/kraken/channels/test_channel/videos",
          "commercial": "https://api.twitch.tv/kraken/channels/test_channel/commercial",
          "chat": "https://api.twitch.tv/kraken/chat/test_channel",
          "features": "https://api.twitch.tv/kraken/channels/test_channel/features"
        },
        "updated_at": "2008-02-12T06:04:29Z",
        "background": null
      }
    },
    ...
  ]
}

频道中的部分将出现 x 次,其中“名称”部分具有不同的值。我将如何使用正则表达式或不获取上面代码中值为“test_channel”的“名称”中的值。它出现的所有时间,然后将其打印到文本框

我认为我管理的唯一部分是正则表达式部分

string regex = @"(""name"":)\s+(\w+)(,""video_banner"")";

【问题讨论】:

  • 是json。使用 json 解析器而不是正则表达式。
  • 您正在寻找有关如何使用此正则表达式在文本字符串中查找匹配项的建议?

标签: c# regex json string text


【解决方案1】:

使用Json.Netthis site

var obj = JsonConvert.DeserializeObject<Krysvac.RootObject>(yourJsonString);
foreach(var item in obj.follows)
{
    Console.WriteLine(item.channel.name);
}

public class Krysvac
{
    public class Links
    {
        public string next { get; set; }
        public string self { get; set; }
    }

    public class Links2
    {
        public string self { get; set; }
    }

    public class Links3
    {
        public string stream_key { get; set; }
        public string self { get; set; }
        public string videos { get; set; }
        public string commercial { get; set; }
        public string chat { get; set; }
        public string features { get; set; }
    }

    public class Channel
    {
        public object banner { get; set; }
        public int _id { get; set; }
        public string url { get; set; }
        public object mature { get; set; }
        public List<object> teams { get; set; }
        public object status { get; set; }
        public object logo { get; set; }
        public string name { get; set; }
        public object video_banner { get; set; }
        public string display_name { get; set; }
        public string created_at { get; set; }
        public int delay { get; set; }
        public object game { get; set; }
        public Links3 _links { get; set; }
        public string updated_at { get; set; }
        public object background { get; set; }
    }

    public class Follow
    {
        public string created_at { get; set; }
        public Links2 _links { get; set; }
        public Channel channel { get; set; }
    }

    public class RootObject
    {
        public Links _links { get; set; }
        public List<Follow> follows { get; set; }
    }
}

如果你不想声明这些类,你也可以使用dynamic关键字

dynamic obj = JsonConvert.DeserializeObject(yourJsonString);
foreach(var item in obj.follows)
{
    Console.WriteLine(item.channel.name);
}

【讨论】:

    【解决方案2】:

    如果您使用json2csharp.com 为您的输入字符串构建如下类,您将获得以下类:

    public class Links
    {
        public string next { get; set; }
        public string self { get; set; }
    }
    
    public class Links2
    {
        public string self { get; set; }
    }
    
    public class Links3
    {
        public string stream_key { get; set; }
        public string self { get; set; }
        public string videos { get; set; }
        public string commercial { get; set; }
        public string chat { get; set; }
        public string features { get; set; }
    }
    
    public class Channel
    {
        public object banner { get; set; }
        public int _id { get; set; }
        public string url { get; set; }
        public object mature { get; set; }
        public List<object> teams { get; set; }
        public object status { get; set; }
        public object logo { get; set; }
        public string name { get; set; }
        public object video_banner { get; set; }
        public string display_name { get; set; }
        public string created_at { get; set; }
        public int delay { get; set; }
        public object game { get; set; }
        public Links3 _links { get; set; }
        public string updated_at { get; set; }
        public object background { get; set; }
    }
    
    public class Follow
    {
        public string created_at { get; set; }
        public Links2 _links { get; set; }
        public Channel channel { get; set; }
    }
    
    public class RootObject
    {
        public Links _links { get; set; }
        public List<Follow> follows { get; set; }
    }
    

    现在您只需将输入 json 字符串反序列化为 RootObject 类,并使用另一个名为 Json.net 的实用程序获取输入字符串中的所有 names

    string json = "JSON STRING";
    RootObject root = JsonConvert.DeserializeObject<RootObject>(json);
    List<string> names = root.follows.Select(follow => follow.channel.name).ToList();
    
    foreach ( string name in names )
    {
        txtBox += name + "; ";
    }
    

    【讨论】:

    • 感谢您的帮助,但由于 L.B 的帖子我解决了它
    • 那么你至少可以投一个赞成票,如果它有效的话;)!谢谢!
    • 我需要 15 声望,但我没有
    • 对不起!我已经对你的问题投了赞成票,所以现在你有 +5 了!
    • a) 我的意图是 class 而不是命名空间。所以这是正确的。请将其作为评论发布,而不是编辑它。 b) root.follows.Select(...) 返回 IEnumerable&lt;string&gt; 而不是 List&lt;string&gt;。添加ToList() 就足够了,但似乎您在发布之前还没有测试过您的代码。 c) 大约 40 分钟后重复我的答案有什么意义?
    【解决方案3】:

    好的,所以我现在可以使用它了,但是,如果我使用我的用户名取回 json,我会得到一大段代码,您可以在此处查看:https://api.twitch.tv/kraken/users/krysvac/follows/channels

    我关注了 31 个人,但是当我在上面使用我的程序和代码时

    using (var w = new WebClient())
                {
                    string json_data = w.DownloadString("https://api.twitch.tv/kraken/users/" + input + "/follows/channels");
    
                    dynamic obj = JsonConvert.DeserializeObject(json_data);
                    foreach (var item in obj.follows)
                    {
                        textBox1.AppendText(item.channel.name.ToString() + Environment.NewLine);                                       
                    }
                }
    

    我在我的 31 个人中得到了 25 个返回到文本框中,我不知道为什么,我在一个应该返回超过 100 个的人身上尝试了它并得到了 6 个返回。

    【讨论】:

    • 您的 API 返回 25 个结果,要获得下一个结果,请使用 (int)obj._totalobj._links.next,即 https://api.twitch.tv/kraken/users/krysvac/follows/channels?direction=DESC&amp;limit=25&amp;offset=25
    • 我还是 c# 的初学者,所以当我要求澄清你的意思时请原谅我
    • 您的 API 在 chunks 中返回结果(每个请求 25 个项目)。使用(int)obj._total,您应该使用循环来获取下一个项目(其网址在obj._links.next
    猜你喜欢
    • 1970-01-01
    • 2018-07-18
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多