带有数据映射器的代码点火器提供有效的json [英] Code igniter with data mapper giving in valid json
本文介绍了带有数据映射器的代码点火器提供有效的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要从表中选择列,并希望将其传递给Jquery ajax fn。
I have this to select columns from a table and I want to pass this to Jquery ajax fn.
我正在使用以下代码,但获取了无效的json
I am using below code but getting invalid json
我的表有三个列ID,名称和城市,但我没有选择城市
My table has three column id, name and city but I am not selecting city
这是我的json响应
["{id:1,name\":\"JOHN\",\"city\":\"null\"}"
,"{\"id\":2,\"name\":\"MICHEAL\,\"city\":\"null\"}"]
推荐答案
当前稳定的(1.8.1)WanWizard数据映射器DMZ_Json类在调用 all_to_json()
时对字段进行双重编码。这个问题似乎已经在开发分支中修复了。您可以通过多种方法解决此问题:
Sadly the current stable (1.8.1) WanWizard datamapper DMZ_Json class double encode fields when you call all_to_json()
. This issue seem to be fixed in the develop branch. You could workaround this multiple ways:
$my_objects = (new Model)->get();
$results = array();
foreach ($my_objects as $o) {
$results[] = $o->to_json();
}
// building a json array from the strings returned by $o->to_json()
print '['.join(',', $results).']';
2。您可以使用数组扩展的 all_to_array
方法和json_encode结果:
2. You can use the array extension's all_to_array
method and json_encode that result:
$my_object_arrays = (new Model)->get()->all_to_array();
print json_encode($my_object_arrays);
这篇关于带有数据映射器的代码点火器提供有效的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文