PHP或JQUERY JSON处理
我有一个来自PHP的简单JSON StdClass对象,我希望将其格式化为table/list/div,并在这个过程中消除其他键和值。JSON如下所示:PHP或JQUERY JSON处理,php,javascript,html,json,web,Php,Javascript,Html,Json,Web,我有一个来自PHP的简单JSON StdClass对象,我希望将其格式化为table/list/div,并在这个过程中消除其他键和值。JSON如下所示: stdClass Object ( [msc] => 150 [number] => 309 [status] => OK [msc_mcc] => 652 [imsi] => 652010154107728 [mcc] => 652 [operator_country] => Botsw
stdClass Object (
[msc] => 150
[number] => 309
[status] => OK
[msc_mcc] => 652
[imsi] => 652010154107728
[mcc] => 652
[operator_country] => Botswana
[msc_operator_name] => MSC
[msc_operator_country] => Botswana
[msc_mnc] => 01
[mnc] => 01
[id] => 1072540715
[msc_location] =>
[operator_name] => MSC )
我已经尝试过PHP并制作了一个表,但问题是我需要选择某些值,而不是整个主体,并且我还需要消除空值
function print_nice($elem,$max_level=10,$print_nice_stack=array()){
if(is_array($elem) || is_object($elem)){
if(in_array($elem,$print_nice_stack,true)){
echo "<font color=red>RECURSION</font>";
return;
}
$print_nice_stack[]=&$elem;
if($max_level<1){
echo "<font color=red>nivel maximo alcanzado</font>";
return;
}
$max_level--;
echo "<table class='table table-bordered table-striped'>";
if(is_array($elem)){
echo '<tr><th colspan=2><strong><font><h3>Results, with love</h3></font></strong></th></tr>';
}else{
echo '<tr><th colspan=2 class=hdrs><strong>';
echo '<font color=white>OBJECT Type: '.get_class($elem).'</font></strong></th></tr>';
}
$color=0;
foreach($elem as $k => $v){
if($max_level%2){
$rgb=($color++%2)?"#f5f5f5":"#efeeee";
}else{
$rgb=($color++%2)?"#f5f5f5":"#efeeee";
}
echo '<tr><td valign="top" style="width:40px;background-color:'.$rgb.';">';
echo '<strong>'.$k."</strong></td><td>";
print_nice($v,$max_level,$print_nice_stack);
echo "</td></tr>";
}
echo "</table>";
return;
}
if($elem === null){
echo "<font color=green>NULL</font>";
}elseif($elem === 0){
echo "0";
}elseif($elem === true){
echo "<font color=green>TRUE</font>";
}elseif($elem === false){
echo "<font color=green>FALSE</font>";
}elseif($elem === ""){
echo "<font color=green>EMPTY STRING</font>";
}else{
echo str_replace("\n","<strong><font color=red>*</font></strong><br>\n",$elem);
}
}
函数print\u nice($elem,$max\u level=10,$print\u nice\u stack=array()){
如果(is_数组($elem)| is_对象($elem)){
如果(在数组中($elem,$print_nice_stack,true)){
回声“递归”;
返回;
}
$print_nice_stack[]=&$elem;
如果($max_级别$v){
如果($max_级别%2){
$rgb=($color++%2)?“#F5”:“#EFEEE”;
}否则{
$rgb=($color++%2)?“#F5”:“#EFEEE”;
}
回声';
回音“”.$k.“”;
打印精美($v、$max\U level、$print\U nice\U stack);
回声“;
}
回声“;
返回;
}
如果($elem==null){
回显“空”;
}elseif($elem==0){
回声“0”;
}elseif($elem==true){
呼应“真”;
}elseif($elem==false){
呼应“假”;
}elseif($elem==“”){
回显“空字符串”;
}否则{
echo str\u replace(“\n”,“*
\n”,$elem);
}
}
在这里可能会有所帮助
例如:
<?php
$object = json_decode($jsonstring);
?>
<table>
<?php
foreach (get_object_vars($object) as $k => $v)
{
if (in_array($k, array('msc', 'number', 'status')) && ! empty($v))
{
echo '<tr>';
echo "<td>{$k}</td><td>{$v}</td>";
echo '</tr>';
}
}
?>
</table>
其中$object
是json_解码变量的名称
编辑:
还添加了空值检查问题到底是什么?我刚刚用我迄今为止尝试过的方法更新了代码,谢谢帮助我假设您正在创建一个类似
$varname=json\u decode($which)的变量
您需要将$varname放入获取对象\u vars($object)
(将$object更改为您的变量)在我的示例中,我不知道从何处获取所有错误,伙计!这在很大程度上要归功于您的解决方案,它的工作非常出色!)