简体   繁体   English

json_decode()-我在做什么错?

[英]json_decode() - What am I doing wrong?

So I decided to make my own helper in Codeigniter to get JSON files and save PokeAPI calls as JSON. 因此,我决定在Codeigniter中创建自己的助手来获取JSON文件并将PokeAPI调用另存为JSON。

The save JSON method I created works fine: 我创建的save JSON方法工作正常:

if ( ! function_exists('saveJson')) {
    function saveJson($file, $data) {
      $fp = fopen($file, 'w');
      fwrite($fp, json_encode($data));
      fclose($fp);
    }
}

However the getJSON function works very randomly. 但是,getJSON函数的工作非常随机。 It works for fetching certain files but others it throws this error: Message: json_decode() expects parameter 1 to be string, array given. 它适用于获取某些文件,但其他则引发此错误: 消息:json_decode()期望参数1为字符串,给定数组。 (all json files are the same format) (所有json文件的格式都相同)

getJSON function: getJSON函数:

if ( ! function_exists('getJson')) {
    function getJson($file) {
      $json = file_get_contents($file);
      $data = json_decode($json, true);
      $pkm = json_decode($data, true);
      return $pkm;
    }
}

Its odd, I have to decode the JSON twice or can I cannot access the array in my views. 奇怪的是,我必须对JSON解码两次,否则我无法在视图中访问数组。

My model and controller for further depth on the issue: Model function example: 我的模型和控制器对此问题有更深入的介绍:模型函数示例:

function getPokemonById($id) {
      $filepath = './assets/jsonsaves/pokemoncalls/'. $id. '.json';
      if(file_exists($filepath)) {
        $pokemonByIdData = getJson($filepath);
      } else {
        $url = $this->pokemonApiAddress.$id.'/';
        $response = Requests::get($url);
        saveJson($filepath, $response);
        $pokemonByIdData = json_decode($response->body, true);
      }
      return $pokemonByIdData;
    }

Controller function example: 控制器功能示例:

public function viewPokemon($id) {
        $singlePokemon['pokemon'] = $this->pokemon_model->getPokemonById($id);
        $singlePokemon['species'] = $this->pokemon_model->getPokemonSpecies($id);
        $data['thepokemon'] = $this->pokemon_model->getAllPokemon();
    $this->load->view('template/header', $data);
        $this->load->view('pokemonpage', $singlePokemon);
    $this->load->view('template/footer');
    }

So there is some variation in my JSON file. 因此,我的JSON文件中有一些变化。 In one JSON file that does not work it has, at the beginning: 在一个不起作用的JSON文件中,开头有:

{"body":"{\"forms\":[{\"url\":\"https:\\\/\\\/pokeapi.co\\\/api\\\/v2\\\/pokemon-form\\\/142\\\/\",\"name\":\"aerodactyl\"}],...

However this one works: 然而,这一工程:

"{\"forms\":[{\"url\":\"https:\\\/\\\/pokeapi.co\\\/api\\\/v2\\\/pokemon-form\\\/6\\\/\",\"name\":\"charizard\"}],...

I fixed the issue thanks to @ccKep. 我通过@ccKep解决了该问题。

I removed the JSON encode from my saveJSON function like so: 我从saveJSON函数中删除了JSON编码,如下所示:

if ( ! function_exists('saveJson')) {
    function saveJson($file, $data) {
      $fp = fopen($file, 'w');
      fwrite($fp, $data);
      fclose($fp);
    }
}

And then removed the second json_decode from my getJSON function: 然后从我的getJSON函数中删除第二个json_decode:

if ( ! function_exists('getJson')) {
    function getJson($file) {
      $json = file_get_contents($file);
      $data = json_decode($json, true);
      return $data;
    }
}

This fixed the errors I was receiving. 这解决了我收到的错误。

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

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