Reputation: 423
I have a
class Product
{
public IComponent[] components;
}
What would be the easiest way to deserialize a Product object from some JSON description like
{
"components": [
{"type":"ComponentA", "key":value}
{"type":"ComponentB", "key":value}
]
}
?
Thanks.
Upvotes: 0
Views: 456
Reputation: 35353
Use Json.Net
string json = JsonConvert.SerializeObject(product);
or use JavaScriptSerializer
string json = new JavaScriptSerializer().Serialize(product);
And the reverse operation
var product = JsonConvert.DeserializeObject<Product>(json)
or
var product = new JavaScriptSerializer().Deserialize<Product>(json)
Upvotes: 2