下面的JSON对象导致ActiveSupport::JSON.decode
抛出以下异常。是什么原因造成的?
例外:
795: unexpected token at ''
/usr/local/lib/ruby/gems/1.9.1/gems/json-1.8.1/lib/json/common.rb:155:in `parse'
/usr/local/lib/ruby/gems/1.9.1/gems/json-1.8.1/lib/json/common.rb:155:in `parse'
/usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.9.2/lib/multi_json/adapters/json_common.rb:16:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.9.2/lib/multi_json/adapter.rb:20:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.9.2/lib/multi_json.rb:121:in `load'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.2.12/lib/active_support/json/decoding.rb:15:in `decode'
JSON对象(来自https://itunes.apple.com/us/lookup?id=972909677):
{
"resultCount":1,
"results": [
{"artistViewUrl":"https://itunes.apple.com/us/artist/twitter-inc./id296415947?uo=4", "artworkUrl60":"http://is2.mzstatic.com/image/pf/us/r30/Purple5/v4/08/c5/cf/08c5cf4e-876f-8518-d23e-6d2e4b24694b/AppIcon60x60_U00402x.png",
"screenshotUrls":["http://a3.mzstatic.com/us/r30/Purple3/v4/13/a1/86/13a18611-ba42-d9fe-38a2-3c5b03db7543/screen1136x1136.jpeg", "http://a2.mzstatic.com/us/r30/Purple3/v4/f3/4c/68/f34c68fe-7284-d094-00ef-49bd66e53851/screen1136x1136.jpeg", "http://a1.mzstatic.com/us/r30/Purple3/v4/9c/1a/c3/9c1ac3c8-cf2c-99cf-87f4-beb9d0962e83/screen1136x1136.jpeg", "http://a3.mzstatic.com/us/r30/Purple6/v4/12/25/a5/1225a5e4-224f-04c6-6ca9-350b4d348aaa/screen1136x1136.jpeg"], "ipadScreenshotUrls":[], "artworkUrl512":"http://is5.mzstatic.com/image/pf/us/r30/Purple3/v4/23/52/7b/23527b98-654b-4dab-9352-4359f1a5e541/mzl.icaayech.jpg", "kind":"software", "features":[],
"supportedDevices":["iPadFourthGen4G", "iPhone6", "iPad23G", "iPhone5s", "iPadMini", "iPhone6Plus", "iPad2Wifi", "iPhone4S", "iPhone5", "iPadFourthGen", "iPhone4", "iPodTouchFifthGen", "iPadThirdGen", "iPadThirdGen4G", "iPadMini4G", "iPhone5c"], "advisories":[], "isGameCenterEnabled":false, "trackCensoredName":"Periscope", "trackViewUrl":"https://itunes.apple.com/us/app/periscope/id972909677?mt=8&uo=4", "contentAdvisoryRating":"4+", "artworkUrl100":"http://is5.mzstatic.com/image/pf/us/r30/Purple3/v4/23/52/7b/23527b98-654b-4dab-9352-4359f1a5e541/mzl.icaayech.jpg", "languageCodesISO2A":["NB", "CA", "CS", "DA", "NL", "EN", "FR", "DE", "EL", "ID", "IT", "JA", "KO", "NN", "PL", "PT", "RU", "ZH", "ES", "SV", "ZH", "TR", "UK", "VI"], "fileSizeBytes":"15090739", "sellerUrl":"https://periscope.tv", "averageUserRatingForCurrentVersion":3.5, "userRatingCountForCurrentVersion":166, "trackContentRating":"4+", "currency":"USD", "wrapperType":"software", "version":"1.0.2",
"description":"Periscope lets you broadcast live video to the world. Going live will instantly notify your followers who can join, comment and send you hearts in real time. The more hearts you get, the higher they flutter on the screen.\n\nOther features:\n[+] REPLAY: When your broadcast is over, you can make it available for replay so viewers can watch later. Viewers can replay your broadcast with comments and hearts to relive the full experience. Replays currently last 24 hours. You can delete your replay at any time.\n[+] PRIVATE: If you want to broadcast to specific people, press the lock icon before going live and choose who you want to invite to your broadcast. \n[+] TWITTER: You can choose to share your Periscope broadcasts on Twitter by tapping the bird icon before you start broadcasting. When you go live, you’ll tweet a link so that your Twitter followers can watch on the web (or in the app)\n[+] MANAGE NOTIFICATIONS: Periscope will suggest people for you to follow based on your Twitter network. You can always follow new people, or unfollow them if you don’t want to be notified when they go live. You can also adjust notification preferences in Periscope Settings (in Profile)\n[+] HEARTS: Periscope keeps track of how many hearts you get from your viewers. The more hearts, the higher you get in the “Most Loved” list.", "artistId":296415947, "artistName":"Twitter, Inc.", "genres":["Social Networking"], "price":0.00, "bundleId":"com.bountylabs.periscope", "genreIds":["6005"], "releaseDate":"2015-03-26T07:01:50Z", "sellerName":"Twitter, Inc.", "trackName":"Periscope", "trackId":972909677, "primaryGenreName":"Social Networking", "primaryGenreId":6005,
"releaseNotes":"- New ‘Global’ section added to Periscope that lists the most recent, live broadcasts from around the world. Your home feed will only show live broadcasts and replays tailored to you.\n- A new “Follower Only” mode is available before starting your broadcast. If you turn this on, only viewers that YOU follow can comment in your broadcast.\n- Users who are verified are marked by the classic Twitter Verified badge you’re used to seeing.\n- Major scrolling performance improvements. This will feel silky and smooth compared to the last version!\n- You can block users more easily (tap on a comment to present the option to block)\n- Fixed a bug where viewers & broadcasters would sometimes stop seeing comments and hearts.\n- Fixed a caching issue where the wrong Profile image would get displayed for a user\n- Fixed a bug where some iOS 7 users couldn’t watch broadcasts with kajillions of viewers", "minimumOsVersion":"7.1", "formattedPrice":"Free", "userRatingCount":949, "averageUserRating":3.5}]
}
请求代码:
id_string = a.join ','
# Make request to iTunes API
uri = URI.parse itunes_url + id_string
http = Net::HTTP.new uri.host, uri.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Get.new uri.request_uri
resp = http.request req
# JSON string -> Hash
logger.info "+++ Decoding #{uri}"
resp_hash = ActiveSupport::JSON.decode resp.body.strip
resp.body.strip
的输出:
]P?NA
??+??@
?u)S ??CΞ/???n?#?)ʿ?="
?m?gF?b??B"x?Ї]?`Nx5z?G+Éo{?Lj?d7|???~?3)???????u?RK?@?????W[8?%r:+?V??Z@+LX?L?TcB?0?e?U?ZQ^Z??ק?-??5BNsj?A?yO
?i?5Н$ʩV-t?sj??_~?nݭ? u?'?l??x
9?3