(require "json") (use-package :json) (json-decode "{\"title\":\"\\u306f\\u3066\\u306a\\u30b9\\u30bf\\u30fc\\u65e5\\u8a18\", \"uri\":\"http://d.hatena.ne.jp/hatenastar/\", \"star_count\":\"75630\"}") ;;=> (("title" . "はてなスター日記") ;; ("uri" . "http://d.hatena.ne.jp/hatenastar/") ;; ("star_count" . "75630")) (json-decode (xhr-get (concat "http://api.awasete.com/showjson.phtml?u=" (si:www-url-encode "http://blog.myrss.jp/")) :key 'xhr-response-text) :strict nil) ;;=> ((("title" . "CSS HappyLife") ;; ("url" . "http://css-happylife.com/") ;; ("favicon" . "http://faviapi.sidetools.com/?url=http%3A%2F%2Fcss-happylife.com%2F&c=4c8a5890") ;; ("navigation" . "http://awasete.com/bar.phtml?u=http%3A%2F%2Fcss-happylife.com%2F&p=http%3A%2F%2Fblog.myrss.jp%2F") ;; ("more" . "http://awasete.com/show.phtml?u=http%3A%2F%2Fcss-happylife.com%2F")) ;; (("title" . "モチベーションは楽しさ創造から") ;; ("url" . "http://d.hatena.ne.jp/favre21/") ;; ("favicon" . "http://faviapi.sidetools.com/?url=http%3A%2F%2Fd.hatena.ne.jp%2Ffavre21%2F&c=e1c17aea") ;; ("navigation" . "http://awasete.com/bar.phtml?u=http%3A%2F%2Fd.hatena.ne.jp%2Ffavre21%2F&p=http%3A%2F%2Fblog.myrss.jp%2F") ;; ("more" . "http://awasete.com/show.phtml?u=http%3A%2F%2Fd.hatena.ne.jp%2Ffavre21%2F")) ;; ;; 省略 ;; )
json は xyzzy Lisp のみで実装した JSON パーサです。 json-syck より高速でかつ外部ライブラリを利用していないので安全(クラッシュすることがない)です。
json はライブラリです。 アプリケーションは以下のコードを追加することで json を利用することができます。
(in-package :you-awesome-app) (require "json") (use-package :json) (your beautiful code)
アーカイブをダウンロードします。
依存ライブラリはありません。
json は以下のパッケージを利用しています。
なし。
なし。
json:json-simple-error
json-parse-error, json-argument-error の親コンディションです。
このコンディション自体が通知されることはありません。
json:json-parse-error
json:json-argument-error
なし。
json:json-decode JSON-TEXT &REST OPTIONS
JSON テキストを読み込み S 式に変換します。
以下のオプションを指定可能です。
厳密に JSON をパースするかどうか指定します。
クォートされていない文字列
(json-decode "{lang:lisp}" :strict t) ;;=> json parse error: bare word not allowed. (json-decode "{lang:lisp}" :strict nil) ;;=> (("lang" . "lisp"))
シングルクォートで囲まれた文字列
(json-decode "{'lang':'lisp'}" :strict t) ;;=> json parse error: single quoted string not allowed. (json-decode "{'lang':'lisp'}" :strict nil) ;;=> (("lang" . "lisp"))
Objects, Arrays の最後に余計なカンマ
(json-decode "{\"lang\":[\"lisp\",\"ruby\",],}" :strict t) ;;=> json parse error: unexpected ']', expecting json value. (json-decode "{\"lang\":[\"lisp\",\"ruby\",],}" :strict nil) ;;=> (("lang" "lisp" "ruby"))
トップレベルの Objects, Arrays の周辺にゴミがある
(json-decode "JSONP({\"lang\":\"lisp\"})" :strict t) ;;=> json parse error: bare word not allowed. (json-decode "JSONP({\"lang\":\"lisp\"})" :strict nil) ;;=> (("lang" . "lisp"))
トップレベルが Objects, Arrays 以外でも受け付ける
(json-decode "true" :strict t) ;;=> json parse error: unexpected bare word, expecting object or array. (json-decode "true" :strict nil) ;;=> t (json-decode "\"hoge\"" :strict t) ;;=> json parse error: unexpected string, expecting object or array. (json-decode "\"hoge\"" :strict nil) ;;=> "hoge"
デフォルトは t です。
JSON の null に対応する lisp の値を指定します。
デフォルト値は nil です。
(json-decode "{\"name\": null}" :json-null :NULL) ;; => (("name" . :NULL))
JSON の true に対応する lisp の値を指定します。
デフォルト値は t です。
(json-decode "{\"xyzzy\": true}" :json-true :TRUE) ;; => (("xyzzy" . :TRUE))
JSON の false に対応する lisp の値を指定します。
デフォルト値は nil です。
(json-decode "{\"xyzzy\": false}" :json-false :FALSE) ;; => (("xyzzy" . :FALSE))
JSON の Arrays のマッピング方法を指定します。
デフォルト値は :list です。
(json-decode "[1, 2, 3]" :json-array :list) ;; => (1 2 3) (json-decode "[1, 2, 3]" :json-array :array) ;; => #(1 2 3)
JSON の Objects のマッピング方法を指定します。
デフォルト値は :alist です。
(json-decode "{\"xyzzy\": \"common lisp\", \"emacs\": \"emacs lisp\"}" :json-object :alist) ;; => (("xyzzy" . "common lisp") ("emacs" . "emacs lisp")) (setf h (json-decode "{\"xyzzy\": \"common lisp\", \"emacs\": \"emacs lisp\"}" :json-object :hash-table)) ;; => #<hashtable 52893588> (gethash "xyzzy" h) ;; => "common lisp" ;; t (gethash "emacs" h) ;; => "emacs lisp" ;; t
hash-table-test も参照してください。
hash-table のテスト関数を指定します。
デフォルトは equal です。
(setf h (json-decode "{\"name\": \"hogehoge\"}" :json-object :hash-table)) ;; => #<hashtable 52893564> (hash-table-test h) ;; => equal (gethash "name" h) ;; => "hogehoge" ;; t (gethash "NaME" h) ;; => nil nil (setf h (json-decode "{\"name\": \"hogehoge\"}" :json-object :hash-table :hash-table-test #'equalp)) ;; => #<hashtable 52893180> (hash-table-test h) ;; => equalp (gethash "name" h) ;; => "hogehoge" ;; t (gethash "NaME" h) ;; => "hogehoge" ;; t
json:json-decode-file FILENAME &REST OPTIONS
指定されたファイルから JSON をロードします。
OPTIONS の指定方法は json-decode を参照してください。
json:json-version
本ライブラリのバージョンを返します。 バージョンは major.minor.teeny という形式です。
それぞれの番号は必ず 1 桁にするので、以下のように比較することができます。
(if (string<= "1.1.0" (json:json-version)) '(1.1.0 以降で有効な処理) '(1.1.0 より前のバージョンでの処理))
なし。
巨大な数値は扱えません。
(json:json-decode "[23456789012E666]") ;;=> json parse error: invalid number: "23456789012E666" (オーバーフローしました)
みやむこ かつゆき (<URL:mailto:miyamuko@gmail.com>)
json は MIT/X ライセンスに基づいて利用可能です。
See json/docs/MIT-LICENSE for full license.
json 0.1.2 リリース!
json 0.1.1 リリース!
json 0.1.0 リリース!