I read from the DB a long json. I want just one attribute of that json.
I have got two options: a. Create an interface for that json and deserialize to that interface.
Regex must be absolutely out of any discussion. Just forget about it, it's as if it never existed.
Creating and working with strong types is a good thing and probably the way I would go.
But if you want, you could also use dynamic
:
class Program
{
static void Main()
{
var json = "{ 'foo': { 'bar': 'bar value', 'baz': [ 1, 2, 3 ] } }";
var serializer = new JavaScriptSerializer();
dynamic value = serializer.DeserializeObject(json);
Console.WriteLine(value["foo"]["baz"][1]);
}
}
prints 2
on the console.