从mysql数据库获取json值并正确显示其元素

时间:2014-01-19 19:14:37

标签: php mysql json wordpress

我的mysql数据库列中有Json值,如下所示

{"2":{"label":"","value":"","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"3":{"label":"location","value":"http:\/\/localhost\/wordpress\/?page_id=320","type":"hidden","validation":"","required":"0","min":"0","max":"1000","tooltip":"","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"4":{"label":"Name","value":"fjedhfjkhre","type":"text","validation":"","required":"0","min":"0","max":"300","tooltip":"field0","custom":"","custom2":null,"custom3":"zz","custom4":null,"custom5":null},"5":{"label":"Email","value":"hjjkhkj@jhjkh.com","type":"email","validation":"email","required":"0","min":"","max":"","tooltip":"field1","custom":"","custom2":"","custom3":"zz","custom4":"","custom5":""},"6":{"label":"Files","value":"3","type":"upload","validation":"file","required":"0","min":"","max":"","tooltip":"field2","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"7":{"label":"files","value":"http:\/\/localhost\/wordpress\/wp-content\/plugins\/formcraft\/file-upload\/server\/php\/files\/Desert.jpg","type":"file0","validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"8":{"label":"files","value":"http:\/\/localhost\/wordpress\/wp-content\/plugins\/formcraft\/file-upload\/server\/php\/files\/Jellyfish.jpg","type":"file1","validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"9":{"label":"files","value":"http:\/\/localhost\/wordpress\/wp-content\/plugins\/formcraft\/file-upload\/server\/php\/files\/Lighthouse.jpg","type":"file2","validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"10":{"label":"Comments","value":"hoi this is a cijkmkmkm","type":"para","validation":"","required":"0","min":"0","max":"300","tooltip":"field3","custom":"","custom2":null,"custom3":"zz","custom4":null,"custom5":null},"11":{"label":"Date","value":"01-15-2014","type":"date","validation":"date","required":"0","min":"","max":"","tooltip":"field4","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"12":{"label":"Profession","value":"Option A","type":"radio","validation":"","required":"0","min":"","max":"","tooltip":"field5","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"13":{"label":"title","value":"Form Title","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"0":{"custom3":"zz","label":"","value":null},"1":{"custom3":"zz","label":"","value":null}}

我想从中获取正确的值,如电子邮件,文件网址等等。

我正在使用它。

   global $wpdb;         
$results = $wpdb->get_results("SELECT * FROM wp_submissions ORDER BY id");       
if(!empty($results)) { 
     foreach($results as $r) {   
          echo "<p>".$r->content."</p>";
          $json_object=$r->content;
            }
            $arr = json_decode($json_object);
 echo echo $arr['0'];
} else {
     echo "<p>Boo, we couldn't find anything that is in all these groups. Try removing a category!</p>";         
} 

当我回显 r-&gt;内容时,它显示如上所述的所有json值。但我无法解码并获得json数组elemt结果。

这是一个表格数据,所以我想在表格中显示企业。

如果有人帮助我,谢谢。

2 个答案:

答案 0 :(得分:0)

JSON的格式看起来很奇怪,但是你不能像数组那样访问它,因为它是一个对象。

例如,该对象的属性“2”是另一个对象;您可以使用

访问它
<?
    //beeing $a that giant json string
    $object = json_decode($a);
    print_r($object->{"2"});
    print_r($object->{"2"}->custom3);
?>

你能试试这个sintax吗?

答案 1 :(得分:0)

尝试了json_decode($ data,true);

$data  = '{"2":{"label":"","value":"","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"3":{"label":"location","value":"http:\/\/localhost\/wordpress\/?page_id=320","type":"hidden","validation":"","required":"0","min":"0","max":"1000","tooltip":"","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"4":{"label":"Name","value":"fjedhfjkhre","type":"text","validation":"","required":"0","min":"0","max":"300","tooltip":"field0","custom":"","custom2":null,"custom3":"zz","custom4":null,"custom5":null},"5":{"label":"Email","value":"hjjkhkj@jhjkh.com","type":"email","validation":"email","required":"0","min":"","max":"","tooltip":"field1","custom":"","custom2":"","custom3":"zz","custom4":"","custom5":""},"6":{"label":"Files","value":"3","type":"upload","validation":"file","required":"0","min":"","max":"","tooltip":"field2","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"7":{"label":"files","value":"http:\/\/localhost\/wordpress\/wp-content\/plugins\/formcraft\/file-upload\/server\/php\/files\/Desert.jpg","type":"file0","validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"8":{"label":"files","value":"http:\/\/localhost\/wordpress\/wp-content\/plugins\/formcraft\/file-upload\/server\/php\/files\/Jellyfish.jpg","type":"file1","validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"9":{"label":"files","value":"http:\/\/localhost\/wordpress\/wp-content\/plugins\/formcraft\/file-upload\/server\/php\/files\/Lighthouse.jpg","type":"file2","validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"10":{"label":"Comments","value":"hoi this is a cijkmkmkm","type":"para","validation":"","required":"0","min":"0","max":"300","tooltip":"field3","custom":"","custom2":null,"custom3":"zz","custom4":null,"custom5":null},"11":{"label":"Date","value":"01-15-2014","type":"date","validation":"date","required":"0","min":"","max":"","tooltip":"field4","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"12":{"label":"Profession","value":"Option A","type":"radio","validation":"","required":"0","min":"","max":"","tooltip":"field5","custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"13":{"label":"title","value":"Form Title","type":null,"validation":null,"required":null,"min":null,"max":null,"tooltip":null,"custom":null,"custom2":null,"custom3":"zz","custom4":null,"custom5":null},"0":{"custom3":"zz","label":"","value":null},"1":{"custom3":"zz","label":"","value":null}}
';

$result = json_decode($data,true);

foreach($result as $key=>$val){
 echo $val["label"];
 // Other elements you can display as well
}

还要确保在执行echo之前检查类似 if(array_key_exists($ val [“label”]))之类的内容,这样可确保在元素不存在的情况下不会发出任何通知数组