Tushar Kanta Prusty Tushar Kanta Prusty - 4 months ago 14x
JSON Question

Not able to access particular data from a List<Rootobject> object

I have a json file, which I have deserialized and added the data to


The json file is as below.


It has 2,00,000 such rows.

I have deserialized it as below.

public List<Rootobject> list=new List<Rootobject>();
public List<Rootobject> ReadAllCities()
var lines = File.ReadAllLines("Assets/city_list.json");
foreach (var line in lines)
var res = JsonConvert.DeserializeObject<Rootobject>(line);
return list;

Now I want to just retrieve the "name" property from the list and create a
object. I am trying to do that using
. But I am getting compilation error.

It says Rootobject does not contain a definition for name.

Please help...


Assuming Rootobject is defined as...

public class Rootobject {
    public int _id { get; set; }
    public string name { get; set; }
    public string country { get; set; }
    public Coord coord { get; set; }

and list is...

public List<Rootobject> list = new List<Rootobject>();

populated with objects.

You can use LINQ to select the name

List<string> namesList = list.Select(i => i.name).ToList();

or if you want to use foreach...

List<string> namesList = new List<string>();
foreach(var item in list) {
    string name = item.name;