反序列化嵌套的complext类型..c#

时间:2017-10-20 09:07:47

标签: c# json serialization

.NET 4.6.1

我有一个json字符串:

{
"success": true,
"rows": [
    {
        "meter_id": "10443720003987688"
    }]
  }

这是我的班级:

public class ESIIDClass
{
    public bool success { get; set; }
    public Rows rows { get; set; }
}

public class Rows
{
    public string meter_id { get; set; }
}

我像这样反序列化字符串:

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
var esiid = json_serializer.Deserialize<ESIIDClass>(theJSONstring);

但这会引发错误:

System.InvalidOperationException: Type 'WebAPI.Controllers.Rows' is not
supported for deserialization of an array.

如何反序列化嵌套的复杂类型?感谢

2 个答案:

答案 0 :(得分:1)

你的类应该被命名为Row,因为它代表一行(它不是pural)。

public class Row

在json中,行显然是一个数组:

"rows": [

所以你的班级应该反映出来:

public Row[] rows { get; set; }

答案 1 :(得分:1)

你可以尝试

public class ESIIDClass
{
    public bool success { get; set; }
    public Rows[] rows { get; set; }
}

public class ESIIDClass
{
    public bool success { get; set; }
    public List<Rows> rows { get; set; }
}

带有[]的json键值对的值将映射从IEnumerable继承的C#类型。