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


i have following text 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 trying parse text, end array each bit of data between {...}

so final result like:

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

then can store these in database

so far have this:

string contents = system.io.file.readalltext("local text file"); //load string contents text file regex regex1 = new regex("\"players\":\\[(?<players>.*)\\]"); //find players section "players":[.......] match match1 = regex1.match(contents); //load match1     regex regex2 = new regex("{(?<player>([^}]*))}"); // break down each player {....} matchcollection match2 = regex2.matches(match1.groups["players"].value); //load match2 each player 

then stuck trying split match string[] somehow , looking @ may overcomplicating it?

any pointer easier solution data parsing

thanks

the data contained in file in json format. json quite simple read, if formatted correctly. if reformat input 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, enclosed in [ ] denotes collection , enclosed in { } denotes object. so, can see have collection called players contains 5 objects (since there 5 pairs of { } within players [ ]) 8 properties each. if think of these in c# terms, have class called player 8 properties, , list<player> hold each player instance. take json data , deserialize them c# counterparts can manipulate them see fit, dave bish has pointed out in answer.

there's easy way create c# classes json data automatically:

  • create new class in project, name want , clear of it's content
  • copy json data (either example or yours)
  • go class , click edit -> paste special -> paste json classes

voila. visual studio's got back. should see along these lines:

public class rootobject {     public player[] players { get; set; } }  public class player {     public int { get; set; }     public int p { get; set; }     public int { 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 whatever suits scenario best, e.g. add system.collections.generic namespace can make player[] list<player> instead on.

now, manipulate json data , deserialize them c# class we've created, can use excellent json.net library. add it, right click application solution explorer , click "manage nuget packages...". type "json.net" in search box , install it.

once have in place, add newtonsoft.json namespace , you're go. can use json.net's deserializeobject<t>() method deserialize json data c# classes we've created:

//i've hardcoded json data here, extract them 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 json c# class created , store in myplayerdata var myplayerdata = jsonconvert.deserializeobject<rootobject>(jsondata);  //you can stuff such as.. foreach(player player in myplayerdata.players) {     messagebox.show(string.format("player {0} has of {1} , of {2}", player.n, player.i, player.a)); } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -