2

I have a JSON file, that I have copied into my project. It contains data like below.

{"_id":707860,"name":"Hurzuf","country":"UA","coord":{"lon":34.283333,"lat":44.549999}} {"_id":519188,"name":"Novinki","country":"RU","coord":{"lon":37.666668,"lat":55.683334}}

It has 20,000 such rows.

These are my classes.

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

public class Coord
{
    public float lon { get; set; }
    public int lat { get; set; }
}

What I want to do is deserialize the json data from the file and put it in a List object.

This is how I am deserializing the data.

var result = JsonConvert.DeserializeObject<Rootobject>(File.ReadAllText("list_city.json"));

It says foreach cannot operate on variables of type RootObject because RootObject doesn't contain public definition for 'GetEnumerator'.

What am I missing here?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Assuming your JSON does not have an outer `[]` that you did not show in your question, this is a duplicate of [Line delimited json serializing and de-serializing](https://stackoverflow.com/questions/29729063) and [Load multiple concatenated JSON objects from stream](https://stackoverflow.com/questions/29477352/load-multiple-concatenated-json-objects-from-stream). See also [Read Multiple Fragments With JsonReader](http://www.newtonsoft.com/json/help/html/ReadMultipleContentWithJsonReader.htm). – dbc Jun 05 '16 at 18:38
  • And also [What is the correct way to use JSON.NET to parse stream of JSON objects?](https://stackoverflow.com/questions/26601594/what-is-the-correct-way-to-use-json-net-to-parse-stream-of-json-objects). – dbc Jun 05 '16 at 18:41
  • By the way, `Coord.lat` should also be a `float` not an `int`. – dbc Jun 05 '16 at 18:43
  • @dbc Changed it to double. – Tushar Kanta Prusty Jun 05 '16 at 20:08

3 Answers3

0

It will depend on the format of the file.

You are trying to deserialize a list. For that you must specify the type to be compatible. Try:

var result = JsonConvert.DeserializeObject<List<Rootobject>>(File.ReadAllText("list_city.json"));

If that doesn't work, and you have all stand alone JSON in each line, you can try:

public IEnumerable<RootObject> ReadAllCities()
{
    var lines = File.ReadAllLines("list_city.json");
    foreach (var line in lines)
    {
       yield return JsonConvert.DeserializeObject<Rootobject>(line);
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Bruno Garcia
  • 6,029
  • 3
  • 25
  • 38
0

I think you have to deserialize this file line by line. The file do not contains any Rootobject collection, array etc. It seems like the whole file do not contains correct json, but each line contains stand-alone Rootobject representation.

So, try something like this:

string[] rows = File.ReadAllLines("Input.txt");
List<Rootobject> rootobjects = new List<Rootobject>();
foreach (string row in rows)
    rootobjects.Add(JsonConvert.DeserializeObject<Rootobject>(row));
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

You need to get the lines and convert it to a Json array so that you can convert it to a list

public class Coord
{
    public double lon { get; set; }
    public double lat { get; set; }
}

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

public class RootObject
{
    public IList<City> cities { get; set; }
}

//get the lines from the file.
var lines = System.IO.File.ReadAllLines("list_city.json");
//format the lines into a proper JSON array
string json = string.Format("{cities:[{0}]}", string.Join(",", lines));

var result = JsonConvert.DeserializeObject<Rootobject>(json);
Nkosi
  • 235,767
  • 35
  • 427
  • 472