在所述字符串中的文本之间获取字符串中的文本 [英] Get text in string between text in said string

查看:100
本文介绍了在所述字符串中的文本之间获取字符串中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个看起来像这样的字符串

Lets say i have a string that looks like this

{
  "_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"值的名称"中的值.一直显示它,然后将其打印到文本框

The part in channel is gonna appear x amount of times with the "name" part having a different value. How would I, using regex or not get the value in "name" that in the code above has a value of "test_channel". All times that it appears and then print it to a textbox

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

The only part I think I've managed is the regex part

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

推荐答案

使用 Json.Net 此网站

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关键字

If you don't want to declare these classes, you can use dynamic keyword too

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

这篇关于在所述字符串中的文本之间获取字符串中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆