2014-11-22 25 views
0

我做以下URL調用返回JSON:Newtonsoft JSON.Net告訴我JSON無效。 JSONLint說,它的確定

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

我再處理結果如下(其中URL包含上述網址):

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

     json = JObject.Parse(response); 
    } 

Parse語句拋出一個異常,告訴我JSON無效。但是,當我將結果粘貼到JSONLint中時,它告訴我它是有效的JSON。錯誤是:

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

通過調用返回的實際JSON如下(直接從VS 2013調試器):

[{"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"}] 

回答

4

問題是你的JSON字符串,而有效的,並不代表一個JSON 對象,它代表一個JSON 數組。因此,要解析它,您需要使用JArray.Parse()JToken.Parse()

+0

就是這樣。謝謝。 – 2014-11-22 19:31:01

+0

沒問題;很高興我能幫上忙。 – 2014-11-22 19:32:21