0

I would appreciate some assistance in a tricky task I have to complete.

I receive a long long string in a text file which look like this :

{"place":"1","points":"1783","pseudo":"player1"},
{"place":"2","points":"1675","pseudo":"player34"},
{"place":"3","points":"1671","pseudo":"player45"},

So this is a single string with about 3000 times the same pattern stuck together. (there are 3000 players)

I would need to parse this string to fill a simple structure like this

public struct RankedPlayer
{
    public string Pseudo;
    public int Place;
    public int Point;
}

I don't find a easy way to do this. I start to struggle with RegEx but I don't know if it's a right approach.

Fedor
  • 1,548
  • 3
  • 28
  • 38
flamandier
  • 502
  • 4
  • 6

2 Answers2

2

Seems like you're dealing with a simple JSON string. Use JSON parson james.newtonking.com - json, and simply get them as your object ...

Here's an example:

// Having this:
public struct RankedPlayer
{
   public string Pseudo;
   public int Place;
   public int Point;
}

// With this input
{"place":"1","points":"1783","pseudo":"player1"},
{"place":"2","points":"1675","pseudo":"player34"},
{"place":"3","points":"1671","pseudo":"player45"},

// You should do something like:
string input = // your input;
var list_of_players = input.Split(',');

foreach (var player in list_of_players) {
    RankedPlayer r = JsonConvert.DeserializeObject<RankedPlayer>(player);
    // Do something with it.
}

So, just use your Player structure.

Edit:

You can use the following for your regex: {.*?} matching. Basically match everything between the curly braces, in a non greedy way (the ? after the .*).

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • I think it's necessary to add attributes to the structure fields because of name mismatches. – Fedor Apr 27 '14 at 10:32
  • Thanks for answer. I didn't know about JSON string ! However there are commas everywhere in the string . If I do : "var list_of_players = input.Split(',');", I won't get a real list of player's substrings. – flamandier Apr 27 '14 at 14:35
  • @Fyodor He might need to do that, true. Or he can do it manually if he prefers. – Noctis Apr 27 '14 at 21:49
  • @flamandier I'm not sure if Newtonsoft can deserialize into a list of objects, I'll have a look a bit later, but if that's the case, you can split on `},` and then just append the `{` to each string inside the foreach... The other option would be to Use a Regex – Noctis Apr 27 '14 at 21:51
  • @Noctis Also, yes, Newtonsoft can serialize directly into collection, no regex needed. – Fedor Apr 28 '14 at 04:42
  • @Fyodor Then problem solved even easier than he thought. Not in front of my dev machine, so couldn't give it a test :). You saved me looking at it later for a follow up :) – Noctis Apr 28 '14 at 05:17
0

You need to use JavaScriptSerializer try:

JavaScriptSerializer serializer = new JavaScriptSerializer();
List<RankedPlayer> myData = serializer.Deserialize<List<RankedPlayer>>(strData);
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110