how to remove $id during JSON serialization

前端 未结 6 1817
萌比男神i
萌比男神i 2020-12-01 09:22

I am using NewtonSoft.JSON. When running

JsonConvert.SerializeObject(myObject)

it is adding an

6条回答
  •  暖寄归人
    2020-12-01 09:48

    The custom ContractResolver setting overrides the PreserveReferencesHandling setting.

    In your implementation of DefaultContractResolver/IContractResolver, add this;

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

    This behaves similarly to the PreserveReferencesHandling.None setting without a custom ContractResolver

提交回复
热议问题