Randy Minder
Randy Minder

Reputation: 48402

Newtonsoft JSON.Net telling me JSON is invalid. JSONLint says it's ok

I'm making the following URL call that returns JSON:

http://ebird.org/ws1.1/data/obs/geo_spp/recent?lng=-83.71307&lat=41.60906&sci=Bubo virginianus&dist=50&back=30&maxResults=100&locale=en_US&fmt=json&includeProvisional=true

I then process the results as follows (where URL contains the URL above):

    using (var webClient = new System.Net.WebClient()) {
        string response = webClient.DownloadString(url.ToString());

        json = JObject.Parse(response);
    }

The Parse statement is throwing an exception telling me the JSON is invalid. However, when I paste the results in JSONLint, it tells me it's valid JSON. The error is:

{"Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."}

The actual JSON returned by the call is as follows (direct from the VS 2013 debugger):

[{"comName":"Great Horned Owl","howMany":2,"lat":41.6830727,"lng":-83.3776689,"locID":"L358647","locName":"Maumee Bay SP","locationPrivate":false,"obsDt":"2014-11-21 17:00","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","howMany":1,"lat":41.6292973,"lng":-83.6956447,"locID":"L586529","locName":"Home","locationPrivate":true,"obsDt":"2014-11-17 03:00","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","howMany":1,"lat":41.3654319,"lng":-83.6707431,"locID":"L628810","locName":"Wintergarden Woods and Saint Johns Nature Preserve","locationPrivate":false,"obsDt":"2014-11-12 14:43","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","howMany":1,"lat":41.616084,"lng":-83.2284737,"locID":"L772841","locName":"Ottawa NWR","locationPrivate":false,"obsDt":"2014-11-11 10:30","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","howMany":1,"lat":41.7437437,"lng":-83.6453748,"locID":"L3092222","locName":"Little Stream","locationPrivate":true,"obsDt":"2014-11-07 08:45","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","howMany":1,"lat":41.373203,"lng":-83.94178,"locID":"L176775","locName":"43534 McClure","locationPrivate":true,"obsDt":"2014-10-27 04:15","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","lat":41.6851559,"lng":-83.3239174,"locID":"L383970","locName":"Cedar Point NWR (restricted access)","locationPrivate":false,"obsDt":"2014-10-25 07:45","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"},{"comName":"Great Horned Owl","howMany":1,"lat":41.6498064,"lng":-83.2374224,"locID":"L211111","locName":"Metzger Marsh Wildlife Area","locationPrivate":false,"obsDt":"2014-10-23 15:05","obsReviewed":false,"obsValid":true,"sciName":"Bubo virginianus"}]

Upvotes: 0

Views: 752

Answers (1)

Brian Rogers
Brian Rogers

Reputation: 129667

The problem is your JSON string, while valid, does not represent a JSON object, it represents a JSON array. So, to parse it you need to use either JArray.Parse() or JToken.Parse().

Upvotes: 5

Related Questions