fnx
fnx

Reputation: 393

Reading values from a string into an array in c#

I'm making an program using C# that can draw polygons and saves their data in a JS file to use in an Javascript application later. I've made the saving to file part, now I need to load the data. Data is stored on a single string like this:

var polys = [
 [{x:0, y:0},{x:2,y:0}, {x:2,y:2}],
 [{x:4, y:4},{x:8,y:4}, {x:8,y:8}, {x:1,y:1}]
];

In the file there are no newlines or whitespace. What is the best method in getting all the inner arrays from that string and turn them into separate C# arrays like this?

Point[] points = new Point[someAmount];

Should I try to parse the string somehow by using regular expressions or are there any built-in methods in C# for this?

Upvotes: 1

Views: 105

Answers (1)

Alden
Alden

Reputation: 6713

Using JSON.NET:

var polygons = JsonConvert.DeserializeObject<List<List<Point>>>(polys);

// polygons is a List<List<Point>>

foreach (var polygon in polygons)
{
    // polygon is a List<Point>
}

Upvotes: 5

Related Questions