2013-04-04 78 views
2

我想將以下JSON表示反序列化爲強類型對象。我能夠從c# - > json序列化它,但反之亦然。爲什麼不能servicestack將此JSON反序列化爲C#?

C#

public class Package 
{ 
    public Guid Id {get;set;} 
    public List<string> Email {get;set;} 
    public List<Items> Items {get;set;} 
} 

public class Items 
{ 
    public string Uri {get;set;} 
    public int Width {get;set;} 
    public int Height {get;set;} 
} 

JSON

{ 
    "Id":"84fd8751-6107-41af-9473-65aae51e042a", 
    "Email":[ 
    "[email protected]" 
    ], 
    "Items":"[ 
     {"Uri":"http://localhost/foo.jpg","Width":234,"Height":313}, 
     {"Uri":"http://localhost/bar.jpg","Width":234,"Height":174}]" 
} 

代碼反序列化

var instance = JsonSerializer.DeserializeFromString<Package>(jsonData); 

目的instance被創建,並且有在instance.Items 2個對象,但所有它們的性質是空值。

TIA

+0

這是我腦死了 - JSON項目的價值是由JSON.stringify(項目)在JavaScript中設置 - 我盯着C#這麼久... – Jason 2013-04-04 19:58:40

回答

6

你周圍的物品價值的報價,從而把它們解析爲一個字符串,而不是一個數組/列表。取消他們贏。

"Items":[ 
     {"Uri":"http://localhost/foo.jpg","Width":234,"Height":313}, 
     {"Uri":"http://localhost/bar.jpg","Width":234,"Height":174}] 
3

這是無效的JSON。項目的值周圍的引號不應該在那裏。

+0

這是有效的JSON,他只是反序列化它到一個不匹配的對象。 – 2013-04-04 19:57:04

+2

@Brad它實際上是無效的JSON。在JSONlint或類似的地方運行它。字符串值將在第二個'''後結束,因此以'Uri ...'開始,該字符串無效。 – 2013-04-04 22:31:11