Newtonsoft JSON.Net告诉我JSON无效。 JSONLint说没关系

时间:2014-11-22 19:18:33

标签: json json.net

我正在进行以下返回JSON的URL调用:

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包含上面的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"}]

1 个答案:

答案 0 :(得分:5)

问题是你的JSON字符串虽然有效,但不代表JSON 对象,它代表一个JSON 数组。因此,要解析它,您需要使用JArray.Parse()JToken.Parse()