2013年2月28日木曜日

今さらながらJSONPath。(PHP)

なぜ今さらながらJSONPathかっていうと、どうにもいい感じにつかえるやつがなかったのです。
http://goessner.net/articles/JsonPath/
LastUpdateが2007年だけど^^;^^;
で、テストコードを書いてみると、ちょっとイイ感じです。
何がイイかって、resultTypeに"PATH"なるものが設定できるところで、この先頭の$を削除してObjectの変数名をつっこみ、
"黒魔術"のevalで、参照したり設定したり。
※javascriptの方も同様にできました。
●テストコード
  1.  require_once "pear/Var_Dump.php";  
  2.  require_once "./jsonpath-0.8.1.php";  
  3.   
  4.  $jstr = file_get_contents('samplejson.txt');  
  5.  $json = json_decode($jstr,true);  
  6.   
  7.  $match1 = jsonPath($json, "$..author");  
  8.  $match2 = jsonPath($json, "$..author", array("resultType" => "PATH"));  
  9.   
  10.  $res1 = Var_Dump::display($match1,true);  
  11.  $res2 = Var_Dump::display($match2,true);  
  12.   
  13.  header( "Content-Type:text/html; charset=utf-8");  
  14.   
  15.  $idxstr = $match2[1];  
  16.  $idxstr = "\x24json" . substr($idxstr,1);  
  17.   
  18.  eval("\x24aa=" . $idxstr . ";");  
  19.  echo "->  {$aa}   
  20. ";  
  21.   
  22.  $idxstr .= "='A123456789';";  
  23.   
  24.  @eval($idxstr); // 失敗してもtry-catchで拾えません^^;  
  25.   
  26.  $res3 = Var_Dump::display($json,true);  
  27.   
  28.  echo "-> " . $idxstr . "  
  29. ";  
  30.   
  31.  echo $res1;  
  32.  echo $res2;  
  33.  echo $res3;  
  34.  exit;  
  35.   
  36. /* 
  37. samplejson.txtの中身。 
  38.  
  39. { "store": { 
  40.     "book": [  
  41.       { "category": "reference", 
  42.         "author": "Nigel Rees", 
  43.         "title": "Sayings of the Century", 
  44.         "price": 8.95 
  45.       }, 
  46.       { "category": "fiction", 
  47.         "author": "Evelyn Waugh", 
  48.         "title": "Sword of Honour", 
  49.         "price": 12.99 
  50.       }, 
  51.       { "category": "fiction", 
  52.         "author": "Herman Melville", 
  53.         "title": "Moby Dick", 
  54.         "isbn": "0-553-21311-3", 
  55.         "price": 8.99 
  56.       }, 
  57.       { "category": "fiction", 
  58.         "author": "J. R. R. Tolkien", 
  59.         "title": "The Lord of the Rings", 
  60.         "isbn": "0-395-19395-8", 
  61.         "price": 22.99 
  62.       } 
  63.     ], 
  64.     "bicycle": { 
  65.       "color": "red", 
  66.       "price": 19.95 
  67.     } 
  68.   } 
  69. } 
  70. */  
  71.   
  72. ?>  
●表示結果
  1. -> Evelyn Waugh   
  2. -> $json['store']['book'][1]['author']='A123456789';  
  3. array(4) {  
  4.   0 => string(10) Nigel Rees  
  5.   1 => string(12) Evelyn Waugh  
  6.   2 => string(15) Herman Melville  
  7.   3 => string(16) J. R. R. Tolkien  
  8. }  
  9. array(4) {  
  10.   0 => string(31) $['store']['book'][0]['author']  
  11.   1 => string(31) $['store']['book'][1]['author']  
  12.   2 => string(31) $['store']['book'][2]['author']  
  13.   3 => string(31) $['store']['book'][3]['author']  
  14. }  
  15. array(1) {  
  16.   store => array(2) {  
  17.     book => array(4) {  
  18.       0 => array(4) {  
  19.         category => string(9) reference  
  20.         author => string(10) Nigel Rees  
  21.         title => string(22) Sayings of the Century  
  22.         price => float 8.95  
  23.       }  
  24.       1 => array(4) {  
  25.         category => string(7) fiction  
  26.         author => string(10) A123456789  
  27. ...略...  

0 件のコメント:

コメントを投稿