在所述字符串中的文本之间获取字符串文本

时间:2014-02-25 16:44:42

标签: c# regex json string text

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

{
  "_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次,“name”部分具有不同的值。我如何使用正则表达式获取“name”中的值,该值在上面的代码中具有值“test_channel”。它出现的所有时间,然后将其打印到文本框

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

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

3 个答案:

答案 0 :(得分:2)

使用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);
}

答案 1 :(得分:0)

如果使用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的实用程序获取输入字符串中的所有name

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

答案 2 :(得分:0)

好的,所以我现在正在使用它,但是,如果我使用我的用户名来取回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个人回来的人。