http://goessner.net/articles/JsonPath/
LastUpdateが2007年だけど^^;^^;
で、テストコードを書いてみると、ちょっとイイ感じです。
何がイイかって、resultTypeに"PATH"なるものが設定できるところで、この先頭の$を削除してObjectの変数名をつっこみ、
"黒魔術"のevalで、参照したり設定したり。
※javascriptの方も同様にできました。
●テストコード
require_once "pear/Var_Dump.php"; require_once "./jsonpath-0.8.1.php"; $jstr = file_get_contents('samplejson.txt'); $json = json_decode($jstr,true); $match1 = jsonPath($json, "$..author"); $match2 = jsonPath($json, "$..author", array("resultType" => "PATH")); $res1 = Var_Dump::display($match1,true); $res2 = Var_Dump::display($match2,true); header( "Content-Type:text/html; charset=utf-8"); $idxstr = $match2[1]; $idxstr = "\x24json" . substr($idxstr,1); eval("\x24aa=" . $idxstr . ";"); echo "-> {$aa}●表示結果
"; $idxstr .= "='A123456789';"; @eval($idxstr); // 失敗してもtry-catchで拾えません^^; $res3 = Var_Dump::display($json,true); echo "-> " . $idxstr . "
"; echo $res1; echo $res2; echo $res3; exit; /* samplejson.txtの中身。 { "store": { "book": [ { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } */ ?>
-> Evelyn Waugh -> $json['store']['book'][1]['author']='A123456789'; array(4) { 0 => string(10) Nigel Rees 1 => string(12) Evelyn Waugh 2 => string(15) Herman Melville 3 => string(16) J. R. R. Tolkien } array(4) { 0 => string(31) $['store']['book'][0]['author'] 1 => string(31) $['store']['book'][1]['author'] 2 => string(31) $['store']['book'][2]['author'] 3 => string(31) $['store']['book'][3]['author'] } array(1) { store => array(2) { book => array(4) { 0 => array(4) { category => string(9) reference author => string(10) Nigel Rees title => string(22) Sayings of the Century price => float 8.95 } 1 => array(4) { category => string(7) fiction author => string(10) A123456789 ...略...
0 件のコメント:
コメントを投稿