2014-01-19 363 views
1

我的Json值在我的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":"[email protected]","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->內容它顯示如上述所有的JSON值。但我無法解碼並獲得json數組elemt結果。

這是一個表格數據,所以我想在表格中顯示enteries。

謝謝如果有人幫助我。

回答

0

該JSON的格式看起來很奇怪,但由於它是一個對象,因此無法像Array一樣訪問它。例如,該對象的屬性「2」是另一個對象;您可以使用

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

你可以試試這個sintax,請接取呢?

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":"[email protected]","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 
} 

還要確保運行檢查類似如果(array_key_exists($ VAL [「標籤」))做回聲在此之前將確保不引發任何通知,如果該元素是不存在的陣列