简体   繁体   中英

c# regex? text string from file and sort into searchable array?

I Have the following text from a file:

{"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}],[.......

What i am trying to do is parse the text, to end up with a array for each bit of data between {...}

so final result would look like:

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

then i can store these in a database

so far i have something like this:

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

then i get stuck trying to split the match string[] somehow and looking at it may be overcomplicating it?

any pointer to an easier solution to the data parsing

Thanks

Your far better off trying to deserialize this with Json.Net from NuGet

Define some classes to match your file structure:

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

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

The data contained in your file are in JSON format. JSON is quite simple to read, if formatted correctly. If I reformat your input the structure becomes clearer:

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

In JSON, anything enclosed in [ ] denotes a collection and anything enclosed in { } denotes an object. So, you can see that you have a collection called players which contains 5 objects (since there are 5 pairs of { } within players [ ] ) with 8 properties each. If you think of these in C# terms, you would have a class called Player with those 8 properties, and a List<Player> to hold each Player instance. You could then take the JSON data and deserialize them into their C# counterparts so you can manipulate them as you see fit, as Dave Bish has pointed out in his answer.

There's a very easy way to create the C# classes from your JSON data automatically:

  • Create a new class in your project, name it however you want and clear all of it's content
  • Copy the JSON data (either from my example or yours)
  • Go back to the class and click Edit -> Paste Special -> Paste JSON As Classes

Voila. Visual Studio's got your back. You should now see something along these lines:

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

You can then do whatever suits your scenario best, eg add the System.Collections.Generic namespace so you can make Player[] a List<Player> instead so on.

Now, to manipulate the JSON data and deserialize them into the C# class we've just created, you can use the excellent Json.NET library. To add it, right click your application from the solution explorer and click "Manage NuGet Packages..." . Type "Json.NET" in the search box and install it.

Once you have that in place, add the Newtonsoft.Json namespace and you're good to go. You can now use Json.NET's DeserializeObject<T>() method to deserialize the JSON data into the C# classes we've created:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM