Trying to parse:
[{"place_id":84979036,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","powered_by":"Map Maker: https://maps.co","osm_type":"node","osm_id":8358755414,"boundingbox":["35.8497801","35.8897801","-114.6800654","-114.6400654"],"lat":"35.8697801","lon":"-114.6600654","display_name":"Willow Beach, Mohave County, Arizona, United States","class":"place","type":"village","importance":0.47500000000000003}]
But the boundingbox:[] in the statement are causing errors, I can't get rid of them so I need to find a way to pull the lat and lon without getting this error.
Error:
Newtonsoft.Json.JsonReaderException: 'Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.'
Code:
bool PullGeoJson(string city, string state, out string[] output)
{
HttpResponse<string> response = UnirestLocationRequest(city, state);
Console.WriteLine(response.Body.ToString());
JObject json = JObject.Parse(response.Body.ToString());
output = new string[2];
output[0] = json.GetValue("lat").ToString();
output[1] = json.GetValue("lon").ToString();
return !(output == null);
}
CodePudding user response:
Create a model that contains the structure of the response you're expecting then deserialize into the model:
static void Main(string[] args)
{
string city = "Pense";
string state = "Canada";
HttpResponse<string> response = Unirest.get("https://geocode.maps.co/search?q=" city "," state).asJson<string>();
var data = JsonConvert.DeserializeObject<List<GeoModel>>(response.Body.ToString());
}
public partial class GeoModel
{
[JsonProperty("place_id")]
public long PlaceId { get; set; }
[JsonProperty("lat")]
public string Lat { get; set; }
[JsonProperty("lon")]
public string Lon { get; set; }
}