2013-04-11 78 views
0

我在數據映射器中使用get()方法從數據庫中選擇列。我通過這個對象json_encode();當我這樣做,我得到這個錯誤帶數據映射器的CodeIgniter ORM

json_encode() type is unsupported, encoded as null 

更新3

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

這是我的回答,我沒有選擇的城市,但仍然出現城市。如何獲得JSON格式正確

語法錯誤:JSON.parse:意想不到的非空白字符JSON數據後

[Break On This Error] 

var jsonData = JSON.parse(data); 
+0

你在模型中使用這個嗎? – Crowlix 2013-04-11 06:44:37

+0

@Crowlix:感謝您花時間回答我的問題。在我的模式下,我有這個代碼類標記擴展DataMapper {}。我應該將application/datamapper包含到我的代碼點火器應用程序文件夾中。可以告訴我,通常情況下,如何使用get()並將該對象傳遞給json_enocde。 – user2261231 2013-04-11 06:54:47

回答

0

編碼一個DataMapper的對象爲JSON對象需要從一個擴展功能。你可以想辦法在這裏導入擴展:http://datamapper.wanwizard.eu/pages/extensions.html 這是做這件事的一種方法:

class Test extends DataMapper { 

    // Include the json extension 
    var $extensions = array('json'); 
....... 

    $test= new Test();     
    $test->where('id', $this->input->post('id')); 
    $test->select('id,name'); 
    $test->get(); 
    $test->set_json_content_type(); 
    echo $test->all_to_json(); 

的「城市」列有正常的事實。從DataMapper的用戶指南:

The object is populated with all objects from its corresponding table, but with only the title and description fields populated

解譯您的JSON,你需要使用這個方法(不要忘記導入分機,如果您還沒有)

$n = new Test(); 
$n->from_json([put your JSON string here]); 
+0

:如果我使用echo $ test-> all_to_json();我在JSON中使用斜槓輸出。現在問題是斜線,如果我刪除斜槓,我很好走。 – user2261231 2013-04-11 08:39:47

+0

順便說一句,你應該刪除你的一些評論。這有點麻煩:p – Crowlix 2013-04-11 08:50:55

+0

:我發佈了更新三到我的問題,仍然存在問題。 – user2261231 2013-04-11 08:53:24

0

我的天堂」 t一起使用的DataMapper的CI,但我敢肯定,你應該做以下幾點:

$test= new Test();     
$test->select('id,tag_name'); 
$test->where('id', $this->input->post('id')); 
$test->get(); 
json_encode($test); 

注:調用 - > get()方法應該是最後一次通話,您使用$測試變量JSON之前對其進行編碼。