【问题标题】:How can I serialize this JSON object?如何序列化这个 JSON 对象?
【发布时间】:2011-02-24 23:25:16
【问题描述】:

我有一个接收 JSON 字符串作为参数的 Web 服务。只有当我的 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"
        }
    ]
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 这让我非常接近..但列表“数据”没有被填充
    • 没关系.. :) 谢谢伙计.. 我不知道 asp.net 会像这样映射属性。
    • @Nick 已更新,删除了私有集并使数据类成为顶级类。如果列表仍然没有反序列化,您也可以尝试用普通数组替换列表
    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 2018-09-08
      • 2019-07-23
      相关资源
      最近更新 更多