3

Why would my object (that doesn't include the $id, be adding it to my Json response?)

Json Response:

enter image description here

Model:

public class DivisionWithProductsViewModel
{
   public int Id {get;set;}
   public string Name {get;set;}
   public string Description {get;set;}
   public string Thumbnail {get;set;}
   public string ThumbnailName {get;set;}
   public List<ProductViewModel> ProductList {get;set;}
}
IronAces
  • 1,857
  • 1
  • 27
  • 36
Ruan
  • 3,969
  • 10
  • 60
  • 87

1 Answers1

3

The $id and $ref fields are used to make an object hierarchy in JSON.

See this JSON for example:

{
   "people":[
      {
         "$id":1,
         "name":"John",
         "children":[
            { "$ref":2 }
         ]
      },
      {
         "$id":2,
         "name":"Jane"
      }
   ]
}

The object in list children will be the exact same as the object used when deserializing Jane.

If you deserialize that using JSON.NET, and you change the name of Jane, it will updated the 'child' Jane too since it is the same object reference.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325