Active Model Serializer格式化JSON显示

时间:2016-08-03 00:55:55

标签: ruby-on-rails json api active-model-serializers

我在rails应用程序中使用Active模型序列化程序,我想重构显示:

每当我去http://localhost:3000/api/users/1时,我都会看到:

{"data":{"id":"1","type":"users","attributes":{"username":"Iggy1"},"relationships":{"items":{"data":[{"id":"1","type":"items"},{"id":"7","type":"items"}]},"lists":{"data":[{"id":"1","type":"lists"},{"id":"8","type":"lists"},{"id":"14","type":"lists"},{"id":"15","type":"lists"},{"id":"17","type":"lists"}]}}}}

如何让它看起来像:

{
    "data": {
        "id": "1",
        "type": "users",
        "attributes": {
            "username": "Iggy1"
        },
        "relationships": {
            "items": {
                "data": [{
                    "id": "1",
                    "type": "items"
                }, {
                    "id": "7",
                    "type": "items"
                }]
            },
            "lists": {
                "data": [{
                    "id": "1",
                    "type": "lists"
                }, {
                    "id": "8",
                    "type": "lists"
                }, {
                    "id": "14",
                    "type": "lists"
                }, {
                    "id": "15",
                    "type": "lists"
                }, {
                    "id": "17",
                    "type": "lists"
                }]
            }
        }
    }
}

我花了很多时间浏览adaptersrenderingarchitecture,但我找不到指南。首先,是否可以使它看起来像上面的第二个代码块?其次,如果可能,我该怎么做才能改变显示?

API / users_controller.rb

   def show
     @user = User.find_by(id: params[:id])
     @no_user_found = User.all #other alternative when user ID not found?
     if @user.nil?
       flash[:notice] = "No user found"
       render json: @no_user_found, each_serializer: UserSerializer
     else
      render json: @user, each_serializer: UserSerializer
    end
   end

user_serializer.rb

class UserSerializer < ActiveModel::Serializer
   attributes :id, :username#, :email
   has_many :items, through: :lists
   has_many :lists
end

的routes.rb

Rails.application.routes.draw do
  ActiveModelSerializers.config.adapter = :json_api
  namespace :api, defaults: { format: :json } do

     resources :users do
       resources :lists
     end

     resources :lists, only: [] do
       resources :items, only: [:create, :index, :show, :update]
     end

     resources :items, only: [:destroy]
   end
end

2 个答案:

答案 0 :(得分:4)

我建议使用Postman而不是使用浏览器 08-02 21:09:35.184 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.hackmodford.bigfinish, PID: 30986 java.lang.IllegalStateException at android.media.MediaPlayer.setPlaybackParams(Native Method) at com.hackmodford.bigfinish.mediaPlayerService.MediaPlayerService.incrementPlaybackRate(MediaPlayerService.java:543) at com.hackmodford.bigfinish.mediaPlayerService.MediaPlayerService.access$1000(MediaPlayerService.java:47) at com.hackmodford.bigfinish.mediaPlayerService.MediaPlayerService$3.onCustomAction(MediaPlayerService.java:336) at android.support.v4.media.session.MediaSessionCompat$Callback$StubApi21.onCustomAction(MediaSessionCompat.java:832) at android.support.v4.media.session.MediaSessionCompatApi21$CallbackProxy.onCustomAction(MediaSessionCompatApi21.java:231) at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1246) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5422) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

您可以使用下面的代码使用缩进和换行来渲染它:

pretty_generate

在上面的代码中使用:

<pre><%= JSON.pretty_generate(your_json_here) %></pre>

答案 1 :(得分:0)

你的问题是关于json输出的样式。正如上面提到的@araratan,您可以使用只为您提供可读性。并且没有必要在机器中使用时尚。 如果您需要Generate preety json outpur

中相同的外观