1

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?

1
  • 2
    What about deserialising from JSON? It should be relatively easy with Newtonsoft JSON.NET package: james.newtonking.com/json Commented Jan 28, 2014 at 12:35

1 Answer 1

5

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>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.