【问题标题】:CodeIginiter, not being able to do a JSON Encode outputCodeIgniter,无法进行 JSON_Encode 输出
【发布时间】:2012-09-09 22:05:22
【问题描述】:

我很确定我错过了一些简单的东西,无论如何,在过去的三个小时里我已经尝试过对一些查询结果进行 JSON 编码,我经常使用代码点火器执行此操作,但是它只是不起作用,我也不知道为什么。

我正在使用 CodeIgniter(最新版本),PHP5。

我的三个文件。

材料模型模型

public function get_types(){
    return $this->db->query("SELECT * FROM material_types ORDER BY type_id")->result_array();
} // end function get_types

public function get_material($name){
    return $this->db->query("SELECT T.name as 'type', M.* FROM material M, material_types T WHERE T.type_id = M.type_id and T.name = '".$name."' ORDER BY M.type_id and M.order")->result_array();
} // end function get_material_all

材料控制器

public function get_all(){
    $json['response'] = array('types' => array());
    $json['response']['types'] = $this->material_model->get_types();


    foreach ($json['response']['types'] as $item){
    $name = $item['name'];

    $json['response'][$name] = $this->material_model->get_material($name);          
    }

    $this->load->view('response', $json);
}

回复查看

$this->output->set_status_header(200);
$this->output->set_header('Content-type: application/json');
echo json_encode($response);

结果 -> http://goethedev.com/iib_ci/index.php/material/get_all/

我使用什么来验证 JSON 页面 -> http://jsonformatter.curiousconcept.com/

我在上面显示整个结果,我已经尝试将其缩小为测试目的,仅 row_array,表 material_types 中只有几个字段,但不走运,这也是我正在工作的新服务器开,这不太可能是问题,但我认为值得一提。

谢谢。

【问题讨论】:

  • 您如何以及在何处使用json 编码数据?
  • 数据将在 Appcelerator Titanium 中使用,但是,在尝试解析它时出现很多错误后,我决定使用 JSON 验证器 jsonformatter.curiousconcept.com,它给我的结果是问题是在服务器端,因为该应用正在从其他来源解析 JSON 数据。
  • Appcelerator Titanium 中你使用的是JSON.parse 吗?
  • 是的,代码从其他来源提取 JSON 编码数据可以正常工作,这非常简单,但如果我尝试从上面提到的 url 执行此操作,它不会返回任何内容。 var xhr = Ti.Network.createHTTPClient(); var url = "goethedev.com/iib_ci/index.php/material/get_all"; xhr.open("GET", url); xhr.send(); xhr.onload = function(e){Ti.API.info(JSON.parse(this.responseText));}

标签: json parsing codeigniter decode encode


【解决方案1】:

从您提供的 URL 返回的 json 正在为我显示得很好,并且它也可以验证。或者您在发布后已修复它,或者您可能需要清除浏览器缓存?

更新

当我将代码复制/粘贴到您提供的验证器时,代码验证得很好,但使用 URL 失败。这告诉我,从浏览器复制/粘贴时未包含额外/错误的内容。

为了识别它,我运行了curl http://goethedev.com/iib_ci/index.php/material/get_all/ | less,这就是我得到的:

<U+FEFF><U+FEFF><U+FEFF>{"types":[{"type_id":"0","name":"DISCLAIMER","timestamp":"2012-09-08 22:57:27"},

&lt;U+FEFF&gt; 代表byte order mark。由于其中有三个,因此您的 CI 项目中的三个文件似乎包含字节顺序标记。我建议像 PSR2 一样,始终禁用 PHP 代码中的字节顺序标记。您的编辑器应该可以选择删除/禁用它们。完成此操作后,您应该能够解析 json。

【讨论】:

  • 它显示得很好,但使用jsonformatter.curiousconcept.com 验证它会给我回复“JSON 数据 URL 不包含 JSON 数据。”,这是正确的,因为我似乎无法解析当我得到结果时,它会在其他地方。
  • 是的,我只是尝试通过jsonlint.com 进行验证,它给了我一个JSON Parse 错误:无法识别的令牌'?',经过一番研究,它给了我字节顺序标记问题,我试试马上解决这个问题,brb。
  • 谢谢你,当我重新保存项目文件时明确指出它应该没有 BOM,它起作用了,我不敢相信我今天花了多少小时,谢谢你帮助。
【解决方案2】:

您的json 看起来不错,但您也可以试试这个

public function get_all(){
    $response['types'] = $this->material_model->get_types();
    foreach ($response['types'] as $item){
        $response['name'][] = $this->material_model->get_material($item['name']);          
    }
    $this->load->view('response', array($response));
}

在客户端试试

xhr.onload = function(e){
    var jsonObject = JSON.parse(xhr.responseText);
    if (jsonObject != null)
    {
        console.log(jsonObject); // see in the console if it prints anything
        Ti.API.info(...); // rest of your code
    }
}

If this helps.

【讨论】:

  • 感谢Sheikh的帮助,不过是字节顺序标记的情况,我在服务器端修改文件后,终于成功了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
  • 2017-07-05
  • 2016-05-30
  • 1970-01-01
  • 2014-10-24
  • 2011-02-23
  • 2021-09-17
相关资源
最近更新 更多