2017-10-20 72 views
0

.NET 4.6.1反序列化嵌套complext types..c#

我有一个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. 

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

+7

不应该'行'是一个集合,因为它是'JSON'中的数组? –

+1

JFYI:也许你应该看看Newtonsoft.Json:https://www.newtonsoft.com/json –

+2

@MartinParkin - 是的..我将行更改为此公开列表 rows {get;组; }它工作。谢谢 – BoundForGlory

回答

1

你的类应该命名为Row,因为它表示一个单独的行(它不是pural)。

public class Row 

在JSON,行显然是一个数组:

"rows": [ 

所以,你的类应该反映:

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

马丁帕金打败你,无论如何 – BoundForGlory

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#类型。