Javascript 如何序列化此JSON对象?

Javascript 如何序列化此JSON对象?,javascript,asp.net,ajax,web-services,jquery,Javascript,Asp.net,Ajax,Web Services,Jquery,我有一个web服务,它接收一个JSON字符串作为参数。只有当我的web方法的参数是泛型类型“object”时,我才能成功发送此消息 我可以将此泛型对象序列化为字符串或自定义对象吗?是否需要修改此方法的参数类型?任何帮助都会很棒 以下是web方法: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string StoreDataOut(object json) { //serialization

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

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

以下是web方法:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(object json)
{
   //serialization magic
}
{
    "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"
        }
    ]
}
这是传递到此web方法的测试JSON:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(object json)
{
   //serialization magic
}
{
    "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"
        }
    ]
}

您需要一个如下所示的类,并将其用作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;}
 }   

您需要一个如下所示的类,并将其用作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;}
 }   
您应该能够让.Net framework正确序列化大多数对象,以便web方法签名如下所示:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(MyClass input);
但我发现某些对象有问题,所以我的回退方法是接受一个字符串,它将是序列化的JSON,然后自己使用类或JSON序列化库之类的反序列化

这是一个使用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);
您应该能够让.Net framework正确序列化大多数对象,以便web方法签名如下所示:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string StoreDataOut(MyClass input);
但我发现某些对象有问题,所以我的回退方法是接受一个字符串,它将是序列化的JSON,然后自己使用类或JSON序列化库之类的反序列化

这是一个使用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);

这让我非常接近。。但是列表数据不会被填充,更不用说谢谢你,伙计。。我不知道asp.net会像那样映射属性。@Nick更新了,删除了私有集,使数据类成为顶级类。如果列表仍然不能反序列化,您也可以尝试用普通数组替换它。这让我非常接近。。但是列表数据不会被填充,更不用说谢谢你,伙计。。我不知道asp.net会像那样映射属性。@Nick更新了,删除了私有集,使数据类成为顶级类。如果列表仍然不能反序列化,您也可以尝试用普通数组替换它