user203212546 user203212546 - 1 month ago 4x
JSON Question

How can I parse a string representation of an array of array of doubles?

I'm passing a JSON object in C#. The original object is an array of arrays of doubles, e.g.

var arrayObject = [[1.2,3,1,0],[2.3,1,0,9],[3,6.7,9,1]]

To pass between JavaScript and C# this has been converted to a JSON representation of an array of arrays:

string json = "[[1.2,3,1,0],[2.3,1,0,9],[3,6.7,9,1]]"

How can I parse this? I want to do something like
, but this gives me an error that "Type 'myObject' is not supported for deserialization of an array."

There's not much structure here in terms of a JSON object...and all I really need to do is parse this out into a set of arrays.
I've read a little about Json.NET, but I don't want to add unnecessary class libraries. Is there a simple way to parse this string?


First, deserialize it into something the serializer does understand - a double[][] - then you can convert it however you want:

string json = "[[1.2,3,1,0],[2.3,1,0,9],[3,6.7,9,1]]";
double[][] arrayOfArrays = new JavaScriptSerializer().Deserialize<double[][]>(json);

Assuming your myObject class looks something like:

class myObject {
    public double[] Arr { get;set; }

Then you could use LINQ to convert it to what you're looking for:

List<myObject> list = arrayOfArrays.Select(x => new myObject { Arr = x }).ToList();