【问题标题】:Some questions regarding JavaScriptSerializer关于 JavaScriptSerializer 的一些问题
【发布时间】:2010-11-11 06:32:07
【问题描述】:
  1. 使用JavaScriptSerializer做序列化时,类的部分字段可以忽略吗?

  2. 在使用JavaScriptSerializer做序列化的时候,可以改变字段名吗? 比如字段是字符串is_OK,但我想映射到isOK?

【问题讨论】:

    标签: c# .net json


    【解决方案1】:

    您可以使用[ScriptIgnore] 跳过属性:

    using System;
    using System.Web.Script.Serialization;
    
    public class Group
    {
        // The JavaScriptSerializer ignores this field.
        [ScriptIgnore]
        public string Comment;
    
        // The JavaScriptSerializer serializes this field.
        public string GroupName;
    }
    

    【讨论】:

      【解决方案2】:

      为了获得最大的灵活性(因为您也提到了名称),理想的做法是在 JavaScriptSerializer 对象上调用 RegisterConverters,注册一个或多个 JavaScriptConverter 实现(可能在数组或迭代器块中)。

      然后,您可以通过将键/值对添加到您返回的字典中来实现 Serialize 以添加(或不添加)任何名称下的值和值。如果数据是双向的,您还需要匹配的 Deserialize,但通常(对于 ajax 服务器)这不是必需的。

      完整示例:

      using System;
      using System.Collections.Generic;
      using System.Web.Script.Serialization;
      class Foo
      {
          public string Name { get; set; }
          public bool ImAHappyCamper { get; set; }
          private class FooConverter : JavaScriptConverter
          {
              public override object Deserialize(System.Collections.Generic.IDictionary<string, object> dictionary, System.Type type, JavaScriptSerializer serializer)
              {
                  throw new NotImplementedException();
              }
              public override System.Collections.Generic.IEnumerable<System.Type> SupportedTypes
              {
                  get { yield return typeof(Foo); }
              }
              public override System.Collections.Generic.IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
              {
                  var data = new Dictionary<string, object>();
                  Foo foo = (Foo)obj;
                  if (foo.ImAHappyCamper) data.Add("isOk", foo.ImAHappyCamper);
                  if(!string.IsNullOrEmpty(foo.Name)) data.Add("name", foo.Name);
                  return data;
              }
          }
          private static JavaScriptSerializer serializer;
          public static JavaScriptSerializer Serializer {
              get {
                  if(serializer == null) {
                      var tmp = new JavaScriptSerializer();
                      tmp.RegisterConverters(new [] {new FooConverter()});
                      serializer = tmp;
                  }
                  return serializer;
              }
          }
      }
      static class Program {
          static void Main()
          {
              var obj = new Foo { ImAHappyCamper = true, Name = "Fred" };
              string s = Foo.Serializer.Serialize(obj);
          }
      }
      

      【讨论】:

        【解决方案3】:

        我会使用匿名类型来保持生成的 JSON 干净。

        class SomeClass {
           public string WantedProperty { get; set; }
           public string UnwantedProperty { get; set; }
        }
        
        var objects = new List<SomeClass>();
        
        ...
        
        new JavaScriptSerializer().Serialize(
           objects
           .Select(x => new {
              x.WantedProperty
           }).ToArray()
        );
        

        【讨论】:

          猜你喜欢
          • 2018-05-24
          • 2013-07-07
          • 2013-05-10
          • 2012-03-18
          • 2023-03-09
          • 2021-07-01
          • 2011-09-27
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多