Php 管理json中的代码和错误
我是编程新手。当我需要向视图发送json时,我有一个任务:Php 管理json中的代码和错误,php,json,php-5.3,Php,Json,Php 5.3,我是编程新手。当我需要向视图发送json时,我有一个任务: { "code" : 200, "error" : 0, "data" : { "sLogo" : "http://test.com/images/logo.png", "aWinners": [ { "sName": "name" } ] } } 我现在知道如何管理数据了,但我不知道如何管理code和error。你
{
"code" : 200,
"error" : 0,
"data" : {
"sLogo" : "http://test.com/images/logo.png",
"aWinners": [
{
"sName": "name"
}
]
}
}
我现在知道如何管理数据了,但我不知道如何管理
code
和error
。你能帮我提一些想法吗?提前谢谢,我的英语很抱歉。我的想法是,我需要将此json发送到视图,但我不明白可以用来获取代码和错误值的MU包含什么
$json = '{"code" : 200,"error" : 0,"data" : {"sLogo" : "http://test.com/images/logo.png","aWinners": [{"sName": "name"}]}}';
$arr = json_decode($json, TRUE);
echo $arr['code'];
echo $arr['error'];
注意:只需将json作为字符串从控制器传递到视图,然后根据需要解码视图中的数据。
内部控制器
鉴于
您能否详细说明如何管理代码和错误
您是否使用框架?在这种情况下,是哪个框架?不,我不使用指定的famework,但是这个项目基于来自Symfonyth的相同组件。我的想法是我需要将这个json发送到视图…但是我不理解代码
必须包含什么
Note: Just pass the json as string from controller to your view and then decode your data in the view as per your requirement.
In Controller
<?php
$json = '{"code" : 200,"error" : 0,"data" : {"sLogo" : "http://test.com/images/logo.png","aWinners": [{"sName": "name"}]}}';
?>
In View
<?php
$arr = json_decode($json, TRUE);
$code= $arr['code'];
$error= $arr['error'];
?>