2011-12-15 268 views
2

IM tyring反序列化我的對象從具有以下格式客戶端來:JSON對象反序列化

{'goalplans': 
    [{"goalPlan":0,"accountId":11,"objectiveId":17,"activity":35,"acctOwner":0,"planDay":"1/30/2013","spend_budget":12,"sortBy":0,"activityFlag":0}, 
    {"goalPlan":0,"accountId":11,"objectiveId":17,"activity":35,"acctOwner":0,"planDay":"1/30/2013","spend_budget":12,"sortBy":0,"activityFlag":0}, 
    {"goalPlan":0,"accountId":11,"objectiveId":17,"activity":35,"acctOwner":0,"planDay":"1/30/2013","spend_budget":12,"sortBy":0,"activityFlag":0}, 
    {"goalPlan":0,"accountId":11,"objectiveId":17,"activity":35,"acctOwner":0,"planDay":"1/30/2013","spend_budget":12,"sortBy":0,"activityFlag":0}, 
    {"goalPlan":0,"accountId":11,"objectiveId":17,"activity":35,"acctOwner":0,"planDay":"1/30/2013","spend_budget":12,"sortBy":0,"activityFlag":0}, 
    {"goalPlan":0,"accountId":11,"objectiveId":17,"activity":35,"acctOwner":0,"planDay":"1/30/2013","spend_budget":12,"sortBy":0,"activityFlag":0} 
]} 

的事情是,我不是能deserialze它在我的服務器端。

請告訴我如何反序列化Json對象到列表對象,如 列表。我嘗試了很多方法,但沒有工作。

回答

4

序列化/反序列化對象/ JSON從存在由於.NET 3.5,試試這個

using System.Web.Script.Serialization; 

var jss = new JavaScriptSerializer(); 
var dict = jss.Deserialize<dynamic>(jsonText); 

詳情請參閱here

希望這有助於

+0

在我看來就像它應該是一個詞典<字符串,列表<詞典<字符串,字符串>>>(也可能是最後一個字符串應該是int,我不記得,如果JavaScriptSerializer會做整型。 ) – Rawling 2011-12-15 12:52:17

+0

編輯過,它只是用於示例@Rawling,在這種情況下,我們根本不需要字典 – 2011-12-15 12:58:30

2

一個可能的第三方組件是JSON 。淨。就我個人而言,我對該庫有很好的體驗(例如使用nuget)

0

我自己解決了這個問題。只需將[Serializable]添加到我的課程 並更改方法以採用班級類型的參數...

//My class 
[Serializable] 
public class GoalPlanList{} 

//method 
public static int GoalplanAddUPD(List<GoalPlanList> goalplans) 
{ 
    foreach (GoalPlanList goals in goalplans) 
    {} 
}