c#正则表达式?文本字符串从文件和排序到可搜索的数组

本文关键字:排序 搜索 数组 文件 正则表达式 文本 字符串 | 更新日期: 2023-09-27 17:51:14

我从一个文件中获得以下文本:

{"players":[{"i":11,"p":0,"a":3186,"n":"IanHx","f":1,"ps":0,"pd":0,"bc":0},{"i":12,"p":0,"a":115,"n":"LoZtamnik","f":1,"ps":0,"pd":0,"bc":0},{"i":58,"p":0,"a":156,"n":"Mr701","f":2,"ps":0,"pd":0,"bc":0},{"i":59,"p":0,"a":156,"n":"B0NE4","f":2,"ps":0,"pd":0,"bc":0},{"i":64,"p":0,"a":324,"n":"5teveJ","f":1,"ps":0,"pd":0,"bc":0}],[.......

我要做的是解析文本,以结束与数组之间的每位数据{…}

所以最终结果看起来像:

i=11
p=0
a=3186
n=IanHx
f=1
ps=0
pd=0
bc=0

那么我可以将这些存储在数据库

到目前为止,我有这样的东西:

string contents = System.IO.File.ReadAllText("local text file"); //load string contents with text file
Regex regex1 = new Regex("'"players'":''[(?<players>.*)'']"); //find the players section "players":[.......]
Match match1 = regex1.Match(contents); //load match1    
Regex regex2 = new Regex("{(?<player>([^}]*))}"); // then break down each player {....}
MatchCollection match2 = regex2.Matches(match1.Groups["players"].Value); //load match2 with each player

然后我卡住了试图分割匹配字符串[]不知何故,看着它可能是过于复杂?

任何指向更简单的数据解析解决方案的指针

谢谢

c#正则表达式?文本字符串从文件和排序到可搜索的数组

您最好尝试用Json对其进行反序列化。Net from NuGet

定义一些类来匹配你的文件结构:

public class Root
{
    public List<Something> players {get; set;}
}
public class Something
{
    public string i {get; set;}
    public string p {get; set;}
    public string a {get; set;}
    public string n {get; set;}
    public string f {get; set;}
    public string ps {get; set;}
    public string pd {get; set;}
    public string bc {get; set;}
}

使用Json。Net to crunch:

var json = @"{""players"":[{""i"":11,""p"":0,""a"":3186,""n"":""IanHx"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":12,""p"":0,""a"":115,""n"":""LoZtamnik"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":58,""p"":0,""a"":156,""n"":""Mr701"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":59,""p"":0,""a"":156,""n"":""B0NE4"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":64,""p"":0,""a"":324,""n"":""5teveJ"",""f"":1,""ps"":0,""pd"":0,""bc"":0}]}";
var data = JsonConvert.DeserializeObject<Root>(json);

文件中包含的数据为JSON格式。如果格式正确,JSON很容易阅读。如果我重新格式化你的输入,结构会变得更清晰:

{
  "players": [
    {
      "i": 11,
      "p": 0,
      "a": 3186,
      "n": "IanHx",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 12,
      "p": 0,
      "a": 115,
      "n": "LoZtamnik",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 58,
      "p": 0,
      "a": 156,
      "n": "Mr701",
      "f": 2,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 59,
      "p": 0,
      "a": 156,
      "n": "B0NE4",
      "f": 2,
      "ps": 0,
      "pd": 0,
      "bc": 0
    },
    {
      "i": 64,
      "p": 0,
      "a": 324,
      "n": "5teveJ",
      "f": 1,
      "ps": 0,
      "pd": 0,
      "bc": 0
    }
  ]
}

在JSON中,[ ]中包含的内容表示集合,{ }中包含的内容表示对象。因此,您可以看到,您有一个名为players的集合,其中包含5个对象(因为players [ ]中有5对{ }),每个对象有8个属性。如果你用c#的术语来考虑这些,你会有一个叫做Player的类,它有这8个属性,还有一个List<Player>来保存每个Player实例。然后你可以将JSON数据反序列化成c#对应的数据,这样你就可以按照你认为合适的方式操作它们,正如Dave Bish在他的回答中指出的那样。

有一种非常简单的方法可以从JSON数据自动创建c#类:

  • 在你的项目中创建一个新类,你可以随意命名它清除所有的内容
  • 复制JSON数据(从我的例子或你的)
  • 回到类,点击Edit -> Paste Special -> Paste JSON As Classes

瞧。Visual Studio支持你。现在您应该看到如下内容:

public class Rootobject
{
    public Player[] players { get; set; }
}
public class Player
{
    public int i { get; set; }
    public int p { get; set; }
    public int a { get; set; }
    public string n { get; set; }
    public int f { get; set; }
    public int ps { get; set; }
    public int pd { get; set; }
    public int bc { get; set; }
}

然后你可以做任何最适合你的场景,例如添加System.Collections.Generic命名空间,这样你就可以将Player[]改为List<Player>,等等。

现在,要操作JSON数据并将它们反序列化到我们刚刚创建的c#类中,您可以使用优秀的Json.NET库。要添加它,请在解决方案资源管理器中右键单击应用程序并单击"Manage NuGet Packages..."。在搜索框中输入"Json.NET"并安装。

准备好之后,添加Newtonsoft.Json名称空间,就可以开始了。现在可以使用Json了。. NET的DeserializeObject<T>()方法将JSON数据反序列化到我们创建的c#类中:

//i've hardcoded the JSON data here, obviously you would extract them from your file
var jsonData = @"{""players"":[{""i"":11,""p"":0,""a"":3186,""n"":""IanHx"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":12,""p"":0,""a"":115,""n"":""LoZtamnik"",""f"":1,""ps"":0,""pd"":0,""bc"":0},{""i"":58,""p"":0,""a"":156,""n"":""Mr701"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":59,""p"":0,""a"":156,""n"":""B0NE4"",""f"":2,""ps"":0,""pd"":0,""bc"":0},{""i"":64,""p"":0,""a"":324,""n"":""5teveJ"",""f"":1,""ps"":0,""pd"":0,""bc"":0}]}";
//deserialize the JSON into the C# class we created and store it in myPlayerData
var myPlayerData = JsonConvert.DeserializeObject<Rootobject>(jsonData);
//you can now do stuff such as..
foreach(Player player in myPlayerData.players)
{
    MessageBox.Show(string.Format("Player {0} has an i of {1} and an a of {2}", player.n, player.i, player.a));
}