【问题标题】:Get json value from mysql database and display its elements properly从 mysql 数据库中获取 json 值并正确显示其元素
【发布时间】:2014-02-08 21:01:18
【问题描述】:

我的 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}}

我想从中获取正确的值,例如电子邮件、文件 url....等。

我正在使用这个。

   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->content 时,它会显示上面的所有 json 值。但我无法解码并获得 json 数组元素结果。

这是一个表单数据,所以我想在表格中显示输入。

感谢有人帮助我。

【问题讨论】:

    标签: php mysql json wordpress


    【解决方案1】:

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

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

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

    请你试试这个sintax好吗?

    【讨论】:

      【解决方案2】:

      试过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"])) 的检查,这将确保如果元素不在其中,则不会发出任何通知数组

      【讨论】:

        猜你喜欢
        • 2021-06-13
        • 1970-01-01
        • 2012-05-27
        • 1970-01-01
        • 2014-04-12
        • 1970-01-01
        • 2021-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多