ラベル JSON の投稿を表示しています。 すべての投稿を表示
ラベル JSON の投稿を表示しています。 すべての投稿を表示

2016年12月9日金曜日

JSON 格式化 Plugin

JSON 格式化 Plugin

Notepad++ Plugin  JSTool

sublime Text Plugin PrettyJSON







2016年3月5日土曜日

Javascript で JSON を送信(Ajax)とPHPサーバの受信のエンコード順番

Javascript で JSON を送信(Ajax)とPHPサーバの受信のエンコード順番

Javascript 側

1.  特殊文字をエンコード
     ["] >>%22  , [&] >> %26にエンコード

2.  encodeURIComponent で 1をエンコードする。
    ["] >> %2522 ,  [&] >> %2526

3.  JSON.stringifyで送信


PHP サーバ側

1.  json_decode

2.  urldecode



確認するためのJavascript
<script type="text/javascript">
        function x(x){

        var e={};
                //データそのまま
                //NG PHP サーバー側 $_REQUEST データの所得失敗、&リンクが切れます。
                //console.info('データそのまま');
                //e.input1 = document.getElementById('input1').value;
                //e.input2 = document.getElementById('input2').value;

                //データをURLエンコード
                //NG PHP サーバー側 json_decode 失敗, " の原因でした。
                //console.info('データをencodeURIComponent');
                //e.input1 = encodeURIComponent(document.getElementById('input1').value);
                //e.input2 = encodeURIComponent(document.getElementById('input2').value);

                //データの特殊文字を%22%26にエンコード
                //NG PHP サーバー側 json_decode 失敗, " の原因でした。
                //console.info('データの特殊文字を%22%26にエンコード');
                //e.input1 = fn_replace(document.getElementById('input1').value);
                //e.input2 = fn_replace(document.getElementById('input2').value);

                //データをURLエンコード、特殊文字を%22%26にエンコード
                console.info('データをURLエンコード、特殊文字を%22%26にエンコード');
                e.input1 = encodeURIComponent(fn_replace(document.getElementById('input1').value));
                e.input2 = encodeURIComponent(fn_replace(document.getElementById('input2').value));


        console.info('#111 origi',e);
        var j = new XMLHttpRequest();
        var url ='/youtube_db/test3.php?';
        j.onreadystatechange = function(){
                if(j.readyState == 4 && j.status == 200){
                        console.info('#222 response', j.responseText);
                }
       }

        j.open('POST',url,true);
        j.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

        //データをJSON.stringifyで送信
        console.info('データをJSON.stringifyで送信');
        console.info('#123 JSON.stringify',JSON.stringify(e));
        j.send('p=' + JSON.stringify(e));

        //データをそのままで送信
        //console.info('データをそのままで送信');
        //console.log('#123 json en',JSON.stringify(e))
        //j.send('p=' + e);

        function fn_replace(q){
                var x = {'&':'%26', '"':'%22'};
                for(var i in x){
                        q= q.replace(RegExp(i,'g'),x[i]);
                }
                        return q;
         }
}



//データそのまま
//NG PHP サーバー側 $_REQUEST データの所得失敗、&リンクが切れます。


//データをURLエンコード
//NG PHP サーバー側 json_decode 失敗, " の原因でした。


//データの特殊文字を%22%26にエンコード
//NG PHP サーバー側 json_decode 失敗, " の原因でした。



//データをURLエンコード、特殊文字を%22%26にエンコード



Javascript 


PHP Server

<?php
//
$x = $_REQUEST;
echo "php server\n";
echo "var_dump()\n";
var_dump($x['p']);
echo "\n";
echo "var_dump(json_decode)\n";
var_dump(json_decode($x['p']));
$temp = json_decode($x['p']);
echo "\n";
echo 'urldecode($temp->input1)'.urldecode($temp->input1);
echo "\n";
echo 'urldecode($temp->input2)'.urldecode($temp->input2);
echo "\n";
echo "\n";



echo "次は\n";
echo "URLencodeしてからjson_decode.";
echo "「\"」先にデコードされる、するとjson_decodeは NULL になります。";
echo "\n";
$y = $_REQUEST;
echo "php server\n";
echo "urldecode\n";
$temp = urldecode($y['p']);
var_dump($temp);
var_dump(json_decode($temp));


?>


'\n' >> '%0d%0a'



      function fn_replace(q){
               var x = {'&':'%26',
                           '"':'%22',
                           '\n':'%0d%0a'
                          };
                      for(var i in x){
                           q= q.replace(RegExp(i,'g'),x[i]);
                        }
                     return q;
               }















2016年2月29日月曜日

PHPサーバーに、javascript ajax でJSON を送信する際に、 「&」「"」のエンコードのメモ

PHPサーバーに、javascript ajax でJSON を送信する際に、
 「&」「"」のエンコードのメモ

他の方法があれば、教えてください。


Javascript 側送信
1.   事前に文字列を置き換

     文字列を書き換えしない場合、送信することが
     できますが PHPサーバー側「&」 リンクが切、「"」は
     文字列エラーになります。
   
     var c = '樂凡&祁隆 - 愛"你一"生';
     JSON.stringify({send:c});     << NG
     c.replace(/&/g,"%26");
     c.replace(/"/g,"%22");

2   encodeURI() でエンコードします。
     c=encodeURI(c);

2.  JSON.stringify で送信します。
   
     JSON.stringify({send:c});





PHPサーバー側受け取る

1.  json_decode で
    JSON エンコードされた文字列を受け取り、
    それを PHP の変数に変換します。

2.  urldecode で
    URL エンコードされた文字列をデコードする。

    $temp = json_decode($p);
    $a = urldecode($temp->send);

 


















2016年2月28日日曜日

JSON.parse ダブルクォーテーションを 解析するとエラーになります。

JSON.parse ダブルクォーテーションを 解析するとエラーになります。
eval 関数以外の方法はないでしょうか?



{"a":"\u6797\u590f\u8587 Rosina Lam \ufe63 \u5f88\u60f3\u8a0e\u53ad\u4f60 I Wish I Could Hate You (TVB\u5287\u96c6\"\u55ae\u6200\u96d9\u57ce\"\u4e3b\u984c\u66f2) (OFFICIAL AUDIO)","b":"\u6a02\u51e1&\u7941\u9686 - \u611b\"\u4f60\u4e00\"\u751f"}













2015年8月29日土曜日

JSON.parse() && JSON.stringify()

JSON.parse()

概要
JSON.parse() メソッドは文字列を JSON として解析し、また任意で解析によって作り出された値を変換します。

構文
JSON.parse(text[, reviver])



JSON.stringify()
概要
JSON.stringify() メソッドは JavaScript の値を JSON 文字列に変換します。置き換え関数を指定して値を置き換えたり、置き換え配列を指定して指定されたプロパティのみを含むようにしたりできます。

構文
JSON.stringify(value[, replacer[, space]])

2015年8月24日月曜日

SQLITE データーベースのデーター追加(途中の記録)

SQLITE データーベースのデーター追加(途中の記録)

<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
//print_r(SQLite3::version());

//
//
//
//


$x = new xtext();
echo json_encode($x->title('title',json_decode($_REQUEST['p'])));

class xtext {
public $db_name = 'xtext.db';
//
public function xtext(){
}

//
public function title($title,$paras){

$para['ad_ip'] = $_SERVER['REMOTE_ADDR'];
$para['ad_agent'] = $this->agent($_SERVER['HTTP_USER_AGENT'],"agent");
$para['ad_time'] = $_SERVER["REQUEST_TIME"];
$para['access_time'] = $_SERVER["REQUEST_TIME"];
$para['set_active'] = '1';
$para['ad_user'] = htmlentities($paras->ad_user);
$para['ad_ps'] =  htmlentities($paras->ad_ps);
$para['title'] =  htmlentities($paras->title);

$table_name = 'title';
$url_id = $this->uniq_id('title');
$this->update($table_name,$url_id,$para);

//test pint print_r($para);
return $url_id;
}


//
public function update($table,$url_id,$para ){
$db = new sqlite3($this->db_name);
foreach ($para as $key=>$val){
$insert = sprintf("update %s set %s = \"%s\" where url_id == \"%s\"" , $table,$key,$val,$url_id);
$db->exec($insert);
}
$db->close();
return 'ture';
}

// Uniquid
public function uniq_id($table_name){
$str = array(range('z','a'),range('Z','A'),range('9','0'));
$length = 11;
$str_r =array();
for ($i=0; $i<$length ;$i++){
$temp = $str[array_rand($str)];
$str_r[count($str_r)] = $temp[array_rand($temp)];
}

$db = new sqlite3($this->db_name);
do{
do{
shuffle($str_r);
$temp_id_str = implode($str_r);
$id_check = $db->querySingle(sprintf("select * from %s where url_id='%s'",$table_name,$temp_id_str),true);
if($id_check){
print_r ( $id_check);
}
}while($id_check);

$insert = sprintf("insert into %s(url_id)VALUES('%s')",$table_name,$temp_id_str);
$check = $db->exec($insert);
if($check){
$db->close();
return $temp_id_str;
}
}while(!$check);

}

// Agent
public function agent($agent,$table_name){
$db = new sqlite3($this->db_name);
$up = sprintf("update %s set count = count +1 where agent == '%s'",$table_name,$agent);
$db->exec($up);
$sch = sprintf("select ROWID from %s where agent == '%s'",$table_name,$agent);
$rowid = $db->querySingle($sch);
if(is_null($rowid)){
$new = sprintf("insert into %s (agent,count)values('%s',1)",$table_name,$agent);
$db->exec($new);
$db->close();
return 1;
}else{
$db->close();
return $rowid;
}
$db->close();
}

}


?>

2015年4月26日日曜日

Youtube API 2.0 サポート終了、API3.0のサーチのために記録

Youtube API 2.0 サポート終了のため、検索結果に「UKY3scPIMd8」
動画が追加されます。 API3.0のOath認証しないサーチのプログラム



<?php
//YoutubeClass.php ver 2
//youtube api 3.0
//https://www.youtube.com/watch?v=UKY3scPIMd8

$g=$_GET['g'];

if($g){
$x=new YoutubeAPI($g);
$x->link();
}else{
exit('88');
}


class youtubeapi {
public $url;
public function youtubeapi($key){
$API_URL = 'https://www.googleapis.com/youtube/v3/search?';
$para['key'] = 'AI********************************Ao';
$para['part'] = 'id';
$para['q'] = $key;
$para['type'] = 'video';
$para['maxResults'] = 3;
$this->url = $API_URL.http_build_query($para);
}

public function link(){
$ll=array();
$data = file_get_contents($this->url);
$xdata = json_decode($data ,true);
foreach ($xdata['items'] as $key=>$val){
$ll[count($ll)]=$val['id']['videoId'];
}
unset ($key);
unset ($val);
echo implode('|',$ll);
}
}

?>