Ryan
Ryan

Reputation: 5546

Json Array - retrieve first record in C#

I am obtaining a JSON array in a dynamic object. How can I obtain the first item( equivalent of First for List collection)? How can I check if Json array is empty

Upvotes: 1

Views: 330

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502246

If that's all you want to do with it, and if the underlying object implements IEnumerable, you could use:

foreach (dynamic item in array)
{
    // Use it here
    break;
}

Or use First explicitly:

dynamic first = Enumerable.First(array);

Upvotes: 3

Related Questions