我试图弄清楚为什么服务器上的 JSON 与使用 Restkit 在我的移动应用程序中收到的不同。当我用 来查看我的对象的 JSON 表示时puts @items.to_json
,我得到以下信息:
[{"item":{"created_at":"2012-04-19T19:42:44Z","id":4,"json_event_id":null,"name":"Biz Cards","theme_id":null,"updated_at":"2012-04-19T19:42:44Z","url":"","user_id":3}},
{"item":{"created_at":"2012-04-20T19:37:33Z","id":5,"json_event_id":null,"name":"Hummus","theme_id":null,"updated_at":"2012-04-20T19:37:33Z","url":"","user_id":3}},
{"item":{"created_at":"2012-04-20T20:01:57Z","id":7,"json_event_id":null,"name":"Wine","theme_id":null,"updated_at":"2012-04-20T20:01:57Z","url":"","user_id":3}},
{"item":{"created_at":"2012-04-25T19:57:33Z","id":9,"json_event_id":null,"name":"this","theme_id":null,"updated_at":"2012-04-25T19:57:33Z","url":"","user_id":3}}]
但是,当我查看在客户端收到的 JSON 时,我会得到一个新的 JSON 元素,其中包含所有空值,包括 ID(此处的最后一个元素):
[{"item":{"created_at":"2012-04-19T19:42:44Z","id":4,"json_event_id":null,"name":"Biz Cards","theme_id":null,"updated_at":"2012-04-19T19:42:44Z","url":"","user_id":3}},
{"item":{"created_at":"2012-04-20T19:37:33Z","id":5,"json_event_id":null,"name":"Hummus","theme_id":null,"updated_at":"2012-04-20T19:37:33Z","url":"","user_id":3}},
{"item":{"created_at":"2012-04-20T20:01:57Z","id":7,"json_event_id":null,"name":"Wine","theme_id":null,"updated_at":"2012-04-20T20:01:57Z","url":"","user_id":3}},
{"item":{"created_at":"2012-04-25T19:57:33Z","id":9,"json_event_id":null,"name":"this","theme_id":null,"updated_at":"2012-04-25T19:57:33Z","url":"","user_id":3}},
{"item":{"created_at":null,"id":null,"json_event_id":null,"name":null,"theme_id":null,"updated_at":null,"url":null,"user_id":3}}]
为什么@items.to_json
返回与 JSON 不同的 JSON format.json { render json: @items }
,我该如何以正确的方式响应@items.to_json
?
编辑 这是填充@items的代码:
@items = current_user.items
编辑 这似乎也只发生在我根据 current_user 填充@items 时:
@items = current_user.items
当我做所有事情时,没有额外的空对象:
@items = Item.all