javascript - Modifying a JSON object returned with PDO -


i'm trying return use json object handlebars. making small todo list learn how use it.

my php api follows :

$query = "select *           table"; try {     $db = getconnection();     $response = $db->query($query);     $todo = $response->fetchall(pdo::fetch_obj);     $bdd = null;     echo json_encode($todo); } 

it returns :

[{"id":"1","todo":"do something","who":"me","is_done":"0"},{"id":"2","todo":"learn json","who":"me","is_done":"0"}] 

but i'd need :

{todos: [{"id":"1","todo":"do something","who":"me","is_done":"0"},{"id":"2","todo":"learn json","who":"me","is_done":"0"}]} 

i tried in php api add instead of echo json_encode($todo)

echo '{todos: ' . json_encode($todo) . '}'; 

but doesn't work. ideas ?

while pointy's answer correct (might want read json), alternatively this:

echo json_encode(array('todos' => $todo)); 

php associative arrays serialized json objects (json_encode() example in manual).


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -