简体   繁体   中英

Rails: change default JSON structure

I'm new in Rails and I use the version 4.2.0.

Let's suppose that we have the following JSON at the url localhost:3000/users

[{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}]

but this is not what I want, I expect something like:

{ users :[
{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}
]}

How can i do? Thanks everybody.

try this in users index controller

render json: {users: @users}

where

@users = [{id: 1, "firstName": "John","lastName": "Smith"},
          {id: 2, "firstName": "John","lastName": "Red"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM