Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string of array of arrays of integers into List<int[]>?

I serialized a javascript array using JSON.stringify and got a string which I used it as:

string test = "[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]";

How do I convert 'test' to a C# List variable?

like image 433
Tony_Henrich Avatar asked Oct 10 '13 01:10

Tony_Henrich


1 Answers

The basic JavaScriptSerializer works for this:

using System.Web.Script.Serialization;

string test = "[[0,2],[0,0],[0,0],[0,0],[0,0],[0,0]]";

var listOfInts = new JavaScriptSerializer()
                     .Deserialize<int[][]>(test)
                     .SelectMany(x => x).ToList();

var listOfArrays = new JavaScriptSerializer()
                       .Deserialize<int[][]>(test)
                       .Select(x => x).ToList();

Not sure whether you wanted a list of arrays or a straight up list of each number.. so I gave both.

like image 189
Simon Whitehead Avatar answered Oct 22 '22 17:10

Simon Whitehead