Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

json.net - how to remove $id during JSON serialization

I am using NewtonSoft.JSON. When running

JsonConvert.SerializeObject(myObject)

it is adding an $id value to my JSON - like this:

  "$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

Can we remove this $id with some JsonSerializerSettings or by any other method?

If yes - then how...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I added this code to my WebApiConfig register method and I got rid of all $id in JSON.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...