c#regex?文件中的文本字符串并排序为可搜索的数组?

时间:2015-05-31 18:14:12

标签: c# regex

我有一个文件中的以下文字:

{"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

然后我试图以某种方式分割匹配字符串[],看着它可能会使它过于复杂?

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

由于

2 个答案:

答案 0 :(得分:4)

尝试使用NuGet中的Json.Net 定义一些类以匹配您的文件结构:

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进行处理:

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

答案 1 :(得分:3)

您文件中包含的数据为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个对象(因为{ }中有5对players [ ]),每个对应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));
}