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

1 回答 1

4

首先我们得到:

数组的反序列化不支持“行”类型。

JSON with[ [显示一个嵌套数组。所以要么改变 JSON,要么做rows一个Row[][]

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

现在我们得到:

ContentPanel1 不是 Int32 的有效值。

出色地...

public int id { get; set; }

对比

""id"": ""ContentPanel1""

现在:"ContentPanel1"不是int. 做id一个string

public string id { get; set; }
于 2012-10-23T10:04:45.257 回答