当然、どー使いたいかを書いてからコーディング。
★使い方のイメージ
$p = new gJPath;
$p->setJson($json);
$patstr = $p->getPathOne("$..author2"); // patString所得
$value = $p->getValueDirect($patstr); // 所得
$p->setValueDirect($patstr,$newValue); // 設定
こんな感じで。できたけど、、これからテスト&デバッグ。
pdfと組版と画像処理とc++とphpとjavascriptとhtml5とcss3とjQueryとjQueriUIとjQueryMobileとRadStudioとAndroidSDK/NDKとWordPressとWelcartを統合するエンジニア的視点。。廃止→as3とflashとflex 追加→柔道
$p = new gJPath;
$p->setJson($json);
$patstr = $p->getPathOne("$..author2"); // patString所得
$value = $p->getValueDirect($patstr); // 所得
$p->setValueDirect($patstr,$newValue); // 設定
こんな感じで。 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
...略...
//-------------------------------------------
{
"doc":{
"pages":{
"layers":[
{
"items":[
{
"name":"abcdefgh",
。。。。データいろいろ。。。。
"items":[
{
。。理論上無限階層下。。データいろいろ。。。。
うん。 //-------------------------------------------
// objectのTREEを下っていって、*['name']=$nameのとき*[$tag]=$valueをセットする件。
//-------------------------------------------
public static function searchAndSet(&$json,$name,$tag,$value){
$ar = array_keys($json);
$n = count($ar);
for($w=0;$w<$n;$w++){
$br = @array_keys( $json[ $ar[$w] ] );
if(count($br) > 0){
$ret = xxxxxxxx::searchAndSet($json[$ar[$w]],$name,$tag,$value);
if($ret != null) return $ret;
}
if($json[ $ar[$w] ]['name'] == $name){
$json[ $ar[$w] ][$tag] = $value;
return $value;
}
}
return null;
}
追記) あ、NAMEが一意なのが前提なコードです。
$url = sprintf('%s://%s%s',
$protocol,
$_SERVER['SERVER_NAME'],
$_SERVER['REQUEST_URI']
);
●改修後$serverName = $_SERVER['HTTP_X_FORWARDED_HOST'];
if($serverName == "") $serverName = $_SERVER['SERVER_NAME'];
$url = sprintf('%s://%s%s',
$protocol,
$serverName,
$_SERVER['REQUEST_URI']
);
いやしかし、リバプロ対応に関する修正って、いままで数限りなくやってるんですが、いまだになくなりません^^;^^;