如何将此 JSON 请求转换为 PHP 中的新 JSON 输出?
我正在尝试将 JSON 请求转换为新的 JSON 输出,因为我需要重命名一些正在使用的变量才能在日历中工作。 http://arshaw.com/fullcalendar/ 该日历附带了一个用于输出 JSON 的示例 PHP 设置。
我如何让 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"
),
));
?>
结果:
[
{
"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 = 我想从我自己的 JSON url 中提取自己的数据,而不是示例 PHP 创建中的数据(但保留示例 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/>
';
}
?>
I am trying to convert a JSON request into a new JSON output, because I need to rename some of the variables being used in order to work in the calendar. http://arshaw.com/fullcalendar/ The calendar came with a sample PHP setup to output JSON.
How would I go about having SAMPLE.PHP echo the data as a loop converting the variable names that are in my JSON file to have the same names?
SAMPLE.PHP = file to setup the Calendar JSON which is called from Jquery
<?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 = I would like to pull my own data from my own JSON url instead of the data in sample PHP creation (but keeping the sample PHP structure). Here is the result of my JSON request, and I have taken out many other rows just to clear up clutter on this post- (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",
}
}
]
}
As a test, I am able to retrieve the values like so just for displaying:
<?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/>
';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接解码传入的 JSON,将其操作为您想要的内容,然后对 JSON 进行编码并打印它?
如果您需要重命名变量(或键),请设置一个
$tmp
变量。也就是说,
所以,我想你的代码看起来像这样:
Why not just decode the incoming JSON, manipulate it to be what you'd like, then encode the JSON and print it?
If you need to rename variables (or keys), then set up a
$tmp
variable.That is,
So, I suppose your code would look something like: