Matvi
Matvi

Reputation: 145

How iterate through json object in c#

im using the next code:

dynamic jsonObj = JsonConvert.DeserializeObject(reader.ReadToEnd());

foreach (var item in jsonObj)

{

 Console.WriteLine("");

}

Where reader.ReadToEnd() has a json string.

But i have an error on foreach,

Error 13 foreach statement cannot operate on variables of type 'System.Collections.IEnumerable' because 'System.Collections.IEnumerable' does not contain a public definition for 'GetEnumerator' C:\Users\myUser\Desktop\app\appRecept\Paises.cs 47 29 appPreregistro

Upvotes: 1

Views: 5529

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 41008

If you're deserializing an array of objects from JSON, something that looks like this:

[
  {blah},
  {blah},
  {blah}
]

then deserialize to a List type:

var jsonObj = JsonConvert.DeserializeObject<List<dynamic>>(reader.ReadToEnd());

Upvotes: 1

Related Questions