2011-06-23 15 views
0

カレンダーで作業するために使用されている変数の名前を変更する必要があるため、JSONリクエストを新しいJSON出力に変換しようとしています。 http://arshaw.com/fullcalendar/カレンダーにはJSONを出力するサンプルPHP設定が付属しています。このJSONリクエストをPHPの新しいJSON出力にどのように変換しますか?

SAMPLE.PHPを使用すると、JSONファイル内の変数名を同じ名前に変換するループとしてデータがエコーされますか?

をsample.phpセットアップに=ファイルのjQueryから呼び出されたJSON

<?php 

$year = date('Y'); 
$month = date('m'); 

echo json_encode(array(

    array(
     'id' => 111, 
     'title' => "Event1", 
     'start' => "$year-$month-10", 
     'url' => "http://www.eventurl.com/1" 
    ), 

    array(
     'id' => 222, 
     'title' => "Event2", 
     'start' => "$year-$month-20", 
     'end' => "$year-$month-22", 
     'url' => "http://www.eventurl.com/2" 
    ), 

)); 

?> 

RESULTカレンダー:

[ 
{ 
    "id": 111, 
    "title": "Event1", 
    "start": "2011-06-10", 
    "url": "http://www.eventurl.com/1" 
}, 
{ 
    "id": 222, 
    "title": "Event2", 
    "start": "2011-06-20", 
    "end": "2011-06-22", 
    "url": "http://eventurl.com/2" 
} 
] 

MYJSONは=私は私から自分のデータを取得したいですサンプルPHPの作成時のデータではなくJSONのURLを所有しています(サンプルPHP構造はそのままです)。ここに私のJSONリクエストの結果である、と私はこの後(myUrl.json)上の混乱を解消するために、他の多くの行を取り出しています

{ 
"events": [ 
    { 
     "event": { 

      "id": 1164038671, 
      "title": "TestEvent", 
      "start_date": "2011-06-24 13:00:00", 
      "end_date": "2011-06-24 16:00:00", 
      "url": "ttp://www.eventurl.com/1", 

     } 
    }, 
    { 
     "event": { 

      "id": 1163896245, 
      "title": "Test", 
      "start_date": "2011-07-14 13:00:00", 
      "end_date": "2011-07-14 16:00:00", 
      "url": "ttp://www.eventurl.com/2", 

     } 
    } 
] 
} 

はテストとして、私が取得できていますこれだけ表示するような値:

<?php 

$string = file_get_contents('myUrl.json'); 
$json_a=json_decode($string,true); 

// array method 
foreach($json_a[events] as $p) 
{ 
echo ' 
ID: '.$p[event][id].' <br/> 
TITLE: '.$p[event][title].' <br/> 
START DATE: '.$p[event][start_date].' <br/> 
END DATE: '.$p[event][end_date].' <br/> 
URL: '.$p[event][url].' <br/> 

<br/><br/> 
'; 

} 
?> 

答えて

2

理由だけではなく、入ってくるJSONをデコードあなたが好きなものをすること、それを操作し、その後、JSONをエンコードし、それを印刷しませんか?

変数(またはキー)の名前を変更する必要がある場合は、変数$tmpを設定します。

<?php 

    $string = file_get_contents('myUrl.json'); 
    $json_a=json_decode($string,true); 
    $json_b = array(); 

    // array method 
    foreach($json_a[events] as $p) 
    { 
     $json_b[event]['ID'] = $json_a[event][id]; 
     $json_b[event]['TITLE'] = $json_a[event][title]; 
     //and so on... 
    } 

    echo json_encode($json_b); 

?> 
:だから、

$json_b['key2'] = $json_a['key1']; 

で、私はあなたのコードは次のようになりますと仮定し

関連する問題