1

I was trying to get data using geonames api and then find only the names of city.

http://api.geonames.org/searchJSON?username=ksuhiyp&country=in&maxRows=10&style=SHORT.

I got the data but it is in json and i could not find a way to get only names of cities in string format. Code used-

var data = (JObject)JsonConvert.DeserializeObject(ResultInCSV);
string cityname= data["name"].Value<string>();
string  cityname= cityname.Split('\n')[0];
//JObject obj = JObject.Parse(ResultInCSV);
//string FirstLine = (string)obj["name"];

Please guide me in getting the name of city from this json.

Json result after clicking on link-

{
    "totalResultsCount": 578584,
    "geonames": [{
            "lng": "72.88261",
            "geonameId": 1275339,
            "countryCode": "IN",
            "name": "Mumbai",
            "toponymName": "Mumbai",
            "lat": "19.07283",
            "fcl": "P",
            "fcode": "PPLA"
        }, {
            "lng": "88.36304",
            "geonameId": 1275004,
            "countryCode": "IN",
            "name": "Kolkata",
            "toponymName": "Kolkata",
            "lat": "22.56263",
            "fcl": "P",
            "fcode": "PPLA"
        }, {
            "lng": "77.59369",
            "geonameId": 1277333,
            "countryCode": "IN",
            "name": "Bengaluru",
            "toponymName": "Bengaluru",
            "lat": "12.97194",
            "fcl": "P",
            "fcode": "PPLA"
        }, {
            "lng": "79",
            "geonameId": 1269750,
            "countryCode": "IN",
            "name": "India",
            "toponymName": "Republic of India",
            "lat": "22",
            "fcl": "A",
            "fcode": "PCLI"
        }, {
            "lng": "80.27847",
            "geonameId": 1264527,
            "countryCode": "IN",
            "name": "Chennai",
            "toponymName": "Chennai",
            "lat": "13.08784",
            "fcl": "P",
            "fcode": "PPLA"
        }, {
            "lng": "77.23149",
            "geonameId": 1273294,
            "countryCode": "IN",
            "name": "Delhi",
            "toponymName": "Delhi",
            "lat": "28.65195",
            "fcl": "P",
            "fcode": "PPLA"
        }, {
            "lng": "78.45636",
            "geonameId": 1269843,
            "countryCode": "IN",
            "name": "Hyderabad",
            "toponymName": "Hyderabad",
            "lat": "17.38405",
            "fcl": "P",
            "fcode": "PPLA"
        }, {
            "lng": "72.58727",
            "geonameId": 1279233,
            "countryCode": "IN",
            "name": "Ahmedabad",
            "toponymName": "Ahmedabad",
            "lat": "23.02579",
            "fcl": "P",
            "fcode": "PPL"
        }, {
            "lng": "73.85535",
            "geonameId": 1259229,
            "countryCode": "IN",
            "name": "Pune",
            "toponymName": "Pune",
            "lat": "18.51957",
            "fcl": "P",
            "fcode": "PPL"
        }, {
            "lng": "77.22445",
            "geonameId": 1261481,
            "countryCode": "IN",
            "name": "New Delhi",
            "toponymName": "New Delhi",
            "lat": "28.63576",
            "fcl": "P",
            "fcode": "PPLC"
        }
    ]
}
4
  • Your JSON contains an array called "geonames" with a list of cities (plural). Your code doesn't work because there's no root element called "name" your choices are "totalResultsCount" and "geonames" Commented Jun 1, 2017 at 4:50
  • I tried 'var data = (JObject)JsonConvert.DeserializeObject(ResultInCSV); string cityname = data["geonames"].Value<string>();' also,still I am not getting.So is there anybetter way to find city names from the json response Commented Jun 1, 2017 at 4:56
  • I'd suggest using an editor like Notepad++ or Sublime to format the JSON into a more readable indented format to help you understand its structure better. Commented Jun 1, 2017 at 5:06
  • Can you mark the answer as the solution if this worked for you. Commented Jun 1, 2017 at 5:24

3 Answers 3

1

The following line of code will give you a list of city names

IList<string> cityNames = data["geonames"].Select(gn => gn["name"].ToString()).ToList();
Sign up to request clarification or add additional context in comments.

Comments

0

You can use any of below method to get the list of city names:

        var jo = JObject.Parse(jsonString);
        var a =jo["geonames"].Select(city => city["name"].ToString()).ToList();

        //Or
        foreach (var cityInfo in jo["geonames"])
        {
            Console.WriteLine(cityInfo["name"]);
        }

Comments

0

You can get this way:

List<string> citynames=new List<string>();
for(i=0;i<data.Length;i++)
{
  citynames.Add(data.geonames[i].name);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.