0

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\"}"]
user2261231
  • 109
  • 2
  • 13
  • What datamapper version are you on? a `print DMZ_VERSION;` should reveal it. – complex857 Apr 11 '13 at 16:48
  • @complex857: v1.8.2.1 http://datamapper.wanwizard.eu/pages/download.html – user2261231 Apr 11 '13 at 16:50
  • @complex857: Can you tell me, Where I am going wrong ? – user2261231 Apr 11 '13 at 16:58
  • I can't reproduce this in my environment, try adding a `var_dump($fields);` [somewhere around here](https://github.com/WanWizard/sparks-datamapper/blob/1.8.2.1/extensions/json.php#L35) to see that your fields are arrive here or not. (btw, `set_json_content_type` method doesn't take arguments) – complex857 Apr 11 '13 at 17:00
  • @complex857:I do not know where class DMZ_Json {} is ? I am using all_to_json().Can you show me a snippet, how to get column from table and pass that to JSON.I removed argument from set_json_content_type but still I get same output. – user2261231 Apr 11 '13 at 17:04
  • I've linked the exact location (file+line) in the github repo, It should be under `application/extensions/json.php` or `application/datamapper/json.php` the function you are looking for is `to_json` on line 33. I don't see any problem with your code snippet in the question though, this is how this should work. All i can offer is a place to debug. – complex857 Apr 11 '13 at 17:07
  • @complex857:I found that class and put var_dump($fields) inside to_json fn. This is what I get array(4) { [0]=> string(2) "id" [1]=> string(10) "name" [2]=> string(15) "city" }array(4) { [0]=> string(2) "id" [1]=> string(10) "name" [2]=> string(15) "city" } – user2261231 Apr 11 '13 at 17:10
  • These are the values passed in to the function? I don't know what's happening here, sorry. You can try `debug_print_backtrace()`-ing and follow the trail of calls to see where could this go wrong. Maybe you have the output generated somewhere else than you think. – complex857 Apr 11 '13 at 17:15
  • @complex857:Where should I put debug_print_backtrace(); Thanks for taking time to help me. I am new to DM so struggling and wasted lot of time but unable to figure this out. – user2261231 Apr 11 '13 at 17:17
  • You can put it to the top of DMZ_Json::all_to_json() method for example, but still, i highly doubt that this is a library bug. – complex857 Apr 11 '13 at 17:19
  • @complex857:If I use to_json(), I am getting response in correct format but say If I have 10 records in my data base, it is only giving first record.How do I get all records ? – user2261231 Apr 11 '13 at 17:20
  • possible duplicate of [Jquery parse xhr.responseText](http://stackoverflow.com/questions/15953325/jquery-parse-xhr-responsetext) – gen_Eric Apr 11 '13 at 17:21
  • 1
    @Rocket Hazmat:I only posted it because I got answer from people saying my json is wrong so I wanted to show , HOw I was getting that. – user2261231 Apr 11 '13 at 17:23
  • Well, the DMZ_Json::all_to_json() should be for that, but it does generate double json_encoded strings (and in your case wrong fields too). I would try the ["array"](https://github.com/WanWizard/sparks-datamapper/blob/1.8.2.1/extensions/array.php) extension and do `json_encode($X->all_to_array(array('id','name')));` instead of the `all_to_json()` call. – complex857 Apr 11 '13 at 17:26
  • @Should i add any thing in my autoload to make all_to_json to work. I replace my echo $X->all_to_json(array('id','name'), TRUE); with your code json_encode($X->all_to_array(array('id','name'))); and getting 'Unable to call the method "all_to_array" on the class – user2261231 Apr 11 '13 at 17:31
  • You need the array extension, so where you have `json` now, just add `array` too, under `application/config/datamapper.php`, you should end up a line like this: `$config['extensions'] = array('array', /* other stuff here ... */);` I've answered your other question http://stackoverflow.com/a/15955338/1515540 that should help. – complex857 Apr 11 '13 at 17:37
  • @complex857:Thanks a lot, my problem got solved. – user2261231 Apr 11 '13 at 17:42
  • @complex857:Post your answer so that I can accept it.Thanks once again. – user2261231 Apr 11 '13 at 17:43

1 Answers1

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);
complex857
  • 20,425
  • 6
  • 51
  • 54