Code igniter with data mapper giving in valid json

﹥>﹥吖頭↗ 提交于 2019-12-25 02:57:38

问题


I have this to select columns from a table and I want to pass this to Jquery ajax fn.

I am using below code but getting invalid json

My table has three column id, name and city but I am not selecting city

This is my json reponse

["{id:1,name\":\"JOHN\",\"city\":\"null\"}"
,"{\"id\":2,\"name\":\"MICHEAL\,\"city\":\"null\"}"]

回答1:


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:

1. Call to_json() on individual model objects and create the json array with string manipulation:

$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. 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);


来源:https://stackoverflow.com/questions/15954174/code-igniter-with-data-mapper-giving-in-valid-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!