2011-02-24 57 views
1

我有一個接收JSON字符串作爲參數的Web服務。當我的web方法的參數是泛型類型的「對象」時,我只能成功發送這個消息。如何序列化這個JSON對象?

我可以將此通用對象序列化爲字符串或自定義對象嗎?我是否需要修改此方法的參數類型?任何幫助都是極好的。

這裏是Web方法:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string StoreDataOut(object json) 
{ 
    //serialization magic 
} 

這是正在傳遞給此Web方法測試JSON:

{ 
    "uid":"1234abcd", 
    "application":"Application Name", 
    "eventName":null, 
    "clienttoken":"Test Client", 
    "version":"1.0.0", 
    "datetime":"1/1/2011 12:00 AM", 
    "data":[ 
     { 
      "id":"alpha_123", 
      "question":"ronunciations in pre-classical times or in non-Attic dialects. For det", 
      "answer":"nunciations " 
     }, 
     { 
     "id":"beta_456", 
     "question":"official documents such as laws an", 
     "answer":"or modif" 
     }, 
     { 
      "id":"gamma_789", 
      "question":" maintained or modified slightly to fit Greek phonology; thus, ?", 
      "answer":"iation of Attic" 
     }, 
     { 
      "id":"delta_098", 
      "question":"econstructed pronunciation of Attic in the late 5th an", 
      "answer":"unciation of " 
     }, 
     { 
      "id":"epsilon_076", 
      "question":"erent stylistic variants, with the descending tail either going straight down o", 
      "answer":"Whole bunch" 
     }, 
     { 
      "id":"zeta_054", 
      "question":"rough breathing when it begins a word. Another diacritic use", 
      "answer":"other dia" 
     } 
    ] 
} 

回答

2

你需要一個類,如下所示,並用其作爲類型在你的webmethod而不是對象。

class JsonDTO 
{ 

    public JsonDTO() 
    { 
    data = new List<data>(); 
    } 
    public string uid {get; set;} 
    public string application {get;set} 
    public string eventName {get; set;} 
    public string clienttoken {get;set} 
    public string version {get;set;} 
    public string @datetime {get; set;} 
    public List<data> data {get; set;} 



} 

public class data 
{ 
    public string id {get; set;} 
    public string question {get; set;} 
    public string answer {get; set;} 
} 
+0

它會工作嗎?!酷..我試試。 – Nick

+0

這讓我非常接近..但名單「數據」沒有得到填充 – Nick

+0

沒關係.. :)謝謝男人..我不知道,asp.net只會映射這樣的屬性。 – Nick

2

應該能夠正確安裝.NET框架連載最對象,以便您的Web方法的簽名是這樣的:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string StoreDataOut(MyClass input); 

但我發現,特定的對象有probelms與,所以我的回退方法是接受一個字符串,而不是(這將是序列化的JSON),並使用JavaScriptSerializer類或JSON序列化庫,如Json.Net我自己反序列化。

這是使用JavaScriptSerializer類在那裏我與處理deserialistion我們的包裝方法分開「實際」的方法進行deserialising對象的例子:

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string StoreDataOut(string input) 
{ 
    var serialiser = new JavaScriptSerializer(); 
    MyClass deserialisedInput = serialiser.Deserialize<MyClass>(input); 
    return (StoreDataOutImpl deserialisedInput); 
} 

private string StoreDataOutImpl(MyClass input); 

這給你的是靈活性能夠使用JavaScriptConverters或使用完全不同的庫(例如Json.Net)來控制序列化。

這將要求您有一個類MyClass,它被正確格式化以接收輸入JSON。如果你不這樣做,那麼你可以讓serializer輸出一個包含對應於序列化JSON對象屬性的鍵 - 值對的字典:

var deserialisedInput = (Dictionary<string, object>)serialiser.DeserializeObject(input);