简体   繁体   English

如何在json中将json编码的数据转换为数组?

[英]How to convert json encoded data to array in codeigniter?

I have a json encoded data stored in my database. 我有一个json编码的数据存储在我的数据库中。 I want to convert this json encoded data to array for specific purpose. 我想将此json编码的数据转换为特定目的的数组。 I have used json_decode function but it seems not working because I got errors. 我使用了json_decode函数,但由于出现错误,它似乎无法正常工作。 How can I get back to array structure the data I have saved in my database in json format? 如何恢复以json格式保存在数据库中的数据的数组结构? Please help. 请帮忙。 Here is my code. 这是我的代码。 Thanks a lot. 非常感谢。

Controller function 控制器功能

function show_at_report(){

      $data = $this->data;
      $report_id = $this->uri->segment(3);
      $at_id = $this->uri->segment(4);
      $query = $this->core_model->get_report_details($report_id,$at_id);
      $a = $query['rows'];
      var_dump($a);


      foreach($a as $b){
       var_dump($b);
      }

    }

Output: 输出:

在此处输入图片说明

Modified Controller function 修改后的控制器功能

function show_at_report(){

      $data = $this->data;
      $report_id = $this->uri->segment(3);
      $at_id = $this->uri->segment(4);
      $query = $this->core_model->get_report_details($report_id,$at_id);
      $a = $query['rows'];
      json_decode($a,TRUE);
      var_dump($a);


      foreach($a as $b){
       json_decode($b,TRUE);
       var_dump($b);
      }

    }

Output showing errors 输出显示错误

在此处输入图片说明

What is the problem with this? 这是什么问题? How can I get the json data back to array? 如何将json数据恢复为数组?

$b is an array, like the error says, json_decode requires a string. $b是一个数组,如错误所示, json_decode需要一个字符串。 And if I'm seeing correctly the variable you really want to decode is $b[0]->data as $b points to an array with 1 object in it, and that object has a public variable called data , this variable contains your json it seems. 而且,如果我正确地看到了您真正想要解码的变量是$b[0]->data因为$b指向其中包含1个对象的数组,并且该对象有一个名为data的公共变量,则该变量包含似乎是json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM