【问题标题】:how to remove $id during JSON serialization如何在 JSON 序列化期间删除 $id
【发布时间】:2012-07-18 13:07:17
【问题描述】:

我正在使用 NewtonSoft.JSON。运行时

JsonConvert.SerializeObject(myObject)

它正在向我的 JSON 添加一个 $id 值 - 像这样:

  "$id": "1",
  "BookingId": 0,
  "CompanyId": 0,
  "IsCashBooking": false,
  "PaymentMethod": 0,
  "IsReferral": false,
  "IsReferralPercent": false,
  "ReferralPaymentType": 0,
  "ReferralDues": 0,
  "PassengerId": 0,
  "DepartmentID": 0,
  "CostCenterID": 0,
  "DeadMiles": 0

我们可以用一些 JsonSerializerSettings 或任何其他方法删除这个 $id 吗?

如果是 - 那么如何...

【问题讨论】:

    标签: json.net


    【解决方案1】:

    我将此代码添加到我的 WebApiConfig 注册方法中,并删除了 JSON 中的所有 $id。

    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
    

    【讨论】:

    • 如果您不需要递归序列化(序列化层次结构),则为有效答案。 $id 用于保留句柄,因此如果元素在某处重复,它不会重复数据,它只会将副本的 $ref 属性设置为原始的 $id。在这种情况下,必须将此属性设置为 Newtonsoft.Json.PreserveReferencesHandling.Objects 并且可以使用 Sajid 的答案。
    • 我已经删除了我的帖子。我在这里添加我的答案作为评论。因此,它可以作为“议会”建议的参考。删除 myObject.$id;就是这样。
    【解决方案2】:

    如果由于某种原因您正在使用自定义 ContractResolver,请查看其他堆栈溢出;

    Json.Net adding $id to EF objects despite setting PreserveReferencesHandling to "None"

    【讨论】:

    • 这对我来说是唯一正确的答案。它在两种情况下解决了这个问题:使用 SQL 服务器提供程序以及 Oracle ODP 提供程序。
    【解决方案3】:

    为我的 Web API 删除 JSON 中的 $id。我为我的类对象添加了 [JsonObject(IsReference = false)],为我的对象类型的属性添加了 [JsonProperty(IsReference = false)]。在我的例子中,RespObj 属性是通用类型 T 并且可以采用我传递给它的任何对象类型,包括集合,所以我不得不使用 [JsonProperty(IsReference = false)] 来摆脱序列化 JSON 字符串中的 $id。

    我没有更改我的 WebApiConfig,因为我使用的是 WEB API 的 MVC 帮助页面,它要求我在我的 webApiconfig 中进行此配置:

    var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
            json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
    

    类对象

    [DataContract(IsReference = true)]
    [JsonObject(IsReference = false)]
    public class QMResponse<T> where T : new()
    {
        public QMResponse()
        {
    
        }
    
        /// <summary>
        /// The response code
        /// </summary> 
        public string RespCode { get; set; }
    
        /// <summary>
        /// The response message
        /// </summary> 
        public string RespMxg { get; set; }
    
        /// <summary>
        /// The exception message
        /// </summary> 
        public string exception { get; set; }
    
        /// <summary>
        /// The object type returned
        /// </summary>         
        [JsonProperty(IsReference = false)]
        public T RespObj { get; set; }
    
        /// <summary>
        /// No of records returned
        /// </summary> 
        public long RecordCount { get; set; }
    
        /// <summary>
        /// The Session Object
        /// </summary> 
        public string session_id { get; set; }
    
    }
    

    【讨论】:

      【解决方案4】:

      如果 'id' 是你的类的属性,那么在其上应用 [JsonIgnore] 属性。 否则,这可能是您问题的答案:

      http://james.newtonking.com/json/help/index.html?topic=html/PreserveObjectReferences.htm

      【讨论】:

      • 如何强制 NewtonSoft 使用id 属性值 对象as $id字段,还是简单地使用id 属性进行引用处理?这似乎是最合乎逻辑的用途,因为允许序列化程序生成它会导致不同的ids,具体取决于被序列化的图形对象的总数。 (如果您单独序列化对象,然后需要将其组装成单个对象并反序列化,这是一个问题)
      • @JoeBrockhaus 关于实现这一目标的信息:stackoverflow.com/questions/9237939/…
      【解决方案5】:

      你可以保留基本配置:

      Newtonsoft.Json.PreserveReferencesHandling.All;
      

      我在我的方法中使用了这种代码格式

      public JsonResult<T> Get()
      {
          return Json(result);
      } 
      

      对我来说很好用。

      【讨论】:

        【解决方案6】:

        自定义 ContractResolver 设置会覆盖 PreserveReferencesHandling 设置。

        在你的 DefaultContractResolver/IContractResolver 实现中,添加这个;

        public override JsonContract ResolveContract(Type type) {
            var contract = base.ResolveContract(type);
            contract.IsReference = false;
            return contract;
        }
        

        这与没有自定义 ContractResolver 的 PreserveReferencesHandling.None 设置类似

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-15
          • 1970-01-01
          • 1970-01-01
          • 2012-11-08
          • 1970-01-01
          • 2012-10-12
          • 2019-01-26
          相关资源
          最近更新 更多