Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
REST Controller JSON collection not returned as array
  • I'm working with a REST controller which is returning a set of records that are being returned in JSON. Since there are multiple records, I assume this meant getting something like this: {
    {
    "id":"0",
    "name":"captain crunch",
    "flavor":"corn starch and naval imperialism"
    }
    {
    "id":"1",
    "name":"cocoa puffs",
    "flavor":"chocolate and awesome"
    }
    } …and that would make Mootools see it as an array of JSON objects. However, I'm getting: {
    "0":{
    "id":"0",
    "name":"captain crunch",
    "flavor":"corn starch and naval imperialism"
    }
    "1":{
    "id":"1",
    "name":"cocoa puffs",
    "flavor":"chocolate and awesome"
    }
    } This is making it so that Mootools doesn't parse it as an array of JSON objects and sees it as just one big object with its own properties. Maybe this is more of a JS question, but how do you even turn that into something I can iterate through? I only need the names, not IDs. The Fuel docs only show an example of what happens when you only get one record.
  • The REST controller uses Format::to_json() to convert the result your controller action returns into json. Internally, that uses Format::to_array() to convert objects to array's. That in return is json encoded. Your result is the standard result generated by json_encode() when you pass it a multidimensional array.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion