Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 在视图上渲染数据?_Json_Codeigniter - Fatal编程技术网

Json 在视图上渲染数据?

Json 在视图上渲染数据?,json,codeigniter,Json,Codeigniter,很抱歉问了这么一个基本的问题。我是JSON新手,对如何在Codeigniter中使用JSON一无所知。 我得到了一些JSON响应,并成功地从controller类在我的页面上回送了它。 代码 function index() { $this->load->spark('restclient/2.1.0'); $this->rest->initialize(array('server' => 'http://someurl.com/')); $this-&g

很抱歉问了这么一个基本的问题。我是JSON新手,对如何在Codeigniter中使用JSON一无所知。
我得到了一些JSON响应,并成功地从controller类在我的页面上回送了它。
代码

function index() 
{
 $this->load->spark('restclient/2.1.0');
 $this->rest->initialize(array('server' => 'http://someurl.com/'));
 $this->rest->option(CURLOPT_SSL_VERIFYPEER, FALSE);
 $repos = $this->rest->get('query?q=someparameter');
 $data['items'] = json_encode($repos, JSON_FORCE_OBJECT);
 echo $data['items']; //works fine
 $this->load->view('index',$data);
}
-ProductsAPI{
   status: 200,
   products: [
        - {
           merchant: "Newegg",
           id:"111",
           img:"someimage.jpg"
          }
        - {
           merchant: "Newegg2",
           id:"112",
           img:"anotherimage.jpg"
          }
JSON结构

function index() 
{
 $this->load->spark('restclient/2.1.0');
 $this->rest->initialize(array('server' => 'http://someurl.com/'));
 $this->rest->option(CURLOPT_SSL_VERIFYPEER, FALSE);
 $repos = $this->rest->get('query?q=someparameter');
 $data['items'] = json_encode($repos, JSON_FORCE_OBJECT);
 echo $data['items']; //works fine
 $this->load->view('index',$data);
}
-ProductsAPI{
   status: 200,
   products: [
        - {
           merchant: "Newegg",
           id:"111",
           img:"someimage.jpg"
          }
        - {
           merchant: "Newegg2",
           id:"112",
           img:"anotherimage.jpg"
          }
问题
当我在视图中执行此操作时,出现了PHP错误。
如何遍历JSON响应?
在将json响应发送到视图之前,是否需要
json\u decode()
我的json响应?

该函数将数组转换为字符串。因此,为了能够以数组(或对象)的形式访问内容,您必须再次访问变量

// decoding json-encoded string into an array
$arr = json_decode($var, true)

// decoding json-encoded string into an object
$obj = json_decode($var)

希望这有帮助

在php中,属性访问操作符是
->
而不是
,如果您的响应已经是JSON,则使用
$data['items']=JSON\u decode($repos)
并将您的产品作为对象数组访问