0

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);
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Sout13
  • 19
  • 2
  • The JSON you posted appears to be create (hint: format it to be human friendly). Therefore I suggest that the body is not quite what you have pasted. What *exactly* is returned from `response.Body.ToString()`? (If you are struggling to understand behaviour the biggest obstacle is often your own assumptions about what works: check *everything*!) – Richard Oct 17 '22 at 12:27
  • Maybe the body is including a BOM? In which case see https://stackoverflow.com/q/51848925/67392 – Richard Oct 17 '22 at 12:34
  • Well, obviously it's not just a json object you got, the [`[` and `]`](https://www.json.org/json-en.html) should have told you as much. Parse the json data as what it _**is**_, not as what you want it to be (because it is not what you want it to be). Then, after parsing, get/extract the desired data from the parsed result... –  Oct 17 '22 at 12:41

1 Answers1

0

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; }

}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 1
    There's no need to create a model - if the OP is happy doing it dynamically, all they need to do is use `JArray.Parse` instead of `JObject.Parse`. – Jon Skeet Oct 17 '22 at 19:59