0

I have an object with children that I'm using JavaScriptSerializer to convert to JSON. I'm using MVC and using the JavaScriptSerializer in a view as so:

@Html.Raw(serializer.Serialize(Model.Designs)

My "designs" object has the children "DesignDeliveries" but I need to remove these from the JSON string so I've tried using projection but cannot get the format I require

So my Designs object is as follows:

[{
"BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
"DesignName": "Design 600",
"Quantity": 100,
"DesignDeliveries": [{
    "BookingId": "3706a896-3b8f-454f-acc8-6540441a3e4a",
    "Quantity": 50,
    "BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
    "DepotId": "9"
},
{
    "BookingId": "3706a896-3b8f-454f-acc8-6540441a3e4a",
    "Quantity": 50,
    "BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
    "DepotId": "18"
}]
}];

However, I need:

[{
"BookingDesignId": "e90e9500-0a6a-4d1b-a82a-fa7ca2d7c034",
"DesignName": "Design 600",
"Quantity": 100,
"DesignDeliveries": [];

Does anyone know how to achieve this?

Thanks in advance

tereško
  • 58,060
  • 25
  • 98
  • 150
ledragon
  • 299
  • 6
  • 17
  • Does it have to be empty, or can you just not serialize it at all? – Nathan Oct 22 '12 at 10:35
  • Hi Nathan...thanks for replying..it needs to be empty. I'm using it to create a knockout js viewmodel. Madman's answer below works. I had tried using Clear() but when projecting to an anonymous type which didn't work. – ledragon Oct 22 '12 at 11:22

1 Answers1

1

Clone Model.Designs, clear your collection of DesignDeliveries, serialize cloned Designs again.

Madman
  • 3,171
  • 2
  • 32
  • 44
  • Thanks Madman...that works a treat...just gotta figure out best way to clone now!. – ledragon Oct 22 '12 at 10:40
  • Here is good solution for cloning objects http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically – Madman Oct 23 '12 at 11:32