如何序列化此JSON对象?

时间:2011-02-24 23:25:16

标签: javascript asp.net ajax web-services jquery

我有一个Web服务,它接收一个JSON字符串作为参数。当我的web方法的参数是泛型类型'object'时,我才能成功发送它。

我可以将此通用对象序列化为字符串或自定义对象吗?我是否需要修改此方法的参数类型?任何帮助都会很棒。

以下是网络方法:

[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 个答案:

答案 0 :(得分:2)

您需要一个如下的类,并将其用作webmethod中的类型而不是object。

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;}
 }   

答案 1 :(得分:2)

能够让.Net框架正确地序列化大多数对象,这样你的web方法签名就像这样:

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

但是我发现它有问题的某些对象,所以我的回退方法是接受一个字符串(这将是序列化的JSON),并使用JavaScriptSerializer类或者自己反序列化它或者一个JSON序列化库,如Json.Net

这是使用JavaScriptSerializer类反序列化对象的示例,其中我将“实际”方法与处理反序列化的包装器方法分开:

[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。如果不这样做,那么您可以让序列化器输出一个字典,该字典将包含与序列化JSON对象的属性相对应的键 - 值对:

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