C # regex? text string from file and sorting into search 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'm trying to do is parse the text to get an array for every bit of data between {...}

so the end result will look like this:

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

      

then i can save them to the 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 [] and it might be too hard to look at?

any pointer to an easier data analysis solution

thank

+3


source to share


2 answers


The data contained in your file is in JSON

. JSON is easy enough 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, everything enclosed in [ ]

represents a collection, and anything enclosed in { }

represents an object. So you can see that you have a collection called players

which contains 5 objects (since players [ ]

there are 5 pairs in it { }

) with 8 properties. If you think of it in C # terms, you will have a class named Player

with these 8 properties and List<Player>

to hold each instance Player

. You can then take the JSON data and deserialize it into your C # maps so you can manipulate it as you see fit, as Dave Bish pointed out in his answer.

There is a very simple way to automatically generate C # classes from your JSON data:

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

Voila. Visual Studio is back. You should now see something like this:



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

      

Then you can do whatever suits your scenario best, eg. add a namespace System.Collections.Generic

so you can do Player[]

a List<Player>

and so on.

Now, to manipulate JSON data and deserialize it into the C # class we just created, you can use the great Json.NET

library. To add it, right click your application in the solution explorer and click "Manage NuGet Packages..."

. Enter "Json.NET"

in the search box and install it.

Once you have that location, add a namespace Newtonsoft.Json

and you're good to go. You can now use the Json.NET method DeserializeObject<T>()

to deserialize JSON data into the C # classes we 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));
}

      

+3


source


You are much 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 for crunching:

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

      

+4


source







All Articles