0
static void Main(string[] args)
{
    var json = @"{ ""rows"": [
                [
                    {
                        ""colspan"": 4,
                        ""id"": ""ContentPanel1""
                    },
                    {
                        ""colspan"": 8,
                        ""id"": ""ContentPanel2""
                    }
                ],
                [
                    {
                        ""colspan"": 12,
                        ""id"": ""ContentPanel3""
                    }
                ]
            ]}";

    var json_serializer = new JavaScriptSerializer();
    var jsonData = json_serializer.Deserialize<Grid>(json);

    Console.ReadKey();
}

[Serializable]
public class Grid
{
    public List<Row> rows { get; set; }
}

[Serializable]
public class Row
{
    public int colspan { get; set; }
    public int id { get; set; }
    public List<Row> rows { get; set; }
}

I am trying to convert this JSON string to a C# object, but I am finding it hard because the error message is not very intuitive. Any JSON punters please help!

ERROR Type 'ConsoleApplication1.Program+Row' is not supported for deserialization of an array.

4
  • 5
    As unintuitive as the error message is, it's even less useful if you don't tell us what it is. Commented Oct 23, 2012 at 10:03
  • Do you have the web essentials for visual studio? madskristensen.net/post/Web-Essentials-2012-released.aspx it has a feature Paste JSON As Classes which makes it awesome to parse Commented Oct 23, 2012 at 10:06
  • I have parsed the JSON using an online JSON raeder and the JSOn seems fine.. Commented Oct 23, 2012 at 10:08
  • You might want to look at JSON.NET and the JObject/JValue features in it, that don't require that you create a fixed structure for your imported JSON, but rather uses dynamic structure values. It's much easier than mapping types exactly especially if you only need a few items out of the data. More info here: west-wind.com/weblog/posts/2012/Aug/30/… Commented Oct 23, 2012 at 10:10

1 Answer 1

4

First we get:

Type 'Row' is not supported for deserialization of an array.

The JSON with [ [ shows a nested array. So either change the JSON, or make rows a Row[][]:

public Row[][] rows { get; set; }

Now we get:

ContentPanel1 is not a valid value for Int32.

well...

public int id { get; set; }

vs

""id"": ""ContentPanel1""

Now: "ContentPanel1" is not an int. Make id a string:

public string id { get; set; }
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.