Active Model Serializer格式化JSON显示

Igg*_*ggy 0 api json ruby-on-rails 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"}]}}}}
Run Code Online (Sandbox Code Playgroud)

我如何使其看起来像:

{
    "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"
                }]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我花了大量时间浏览适配器渲染体系结构,但找不到该指南。首先,是否可以使其看起来像上面的第二个代码块?其次,如果可能,我该怎么做才能更改显示?

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
Run Code Online (Sandbox Code Playgroud)

user_serializer.rb

class UserSerializer < ActiveModel::Serializer
   attributes :id, :username#, :email
   has_many :items, through: :lists
   has_many :lists
end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

oj5*_*5th 5

我建议不要使用浏览器,而应使用Postman pretty_generate

您可以使用以下代码使用缩进和换行符来呈现它:

<pre><%= JSON.pretty_generate(your_json_here) %></pre>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,执行以下操作:

<%
  require 'json'

  hash = JSON[{"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"}]}}}}.to_json] 
%>

<pre>
  <%=  JSON.pretty_generate(hash)%>
</pre>
Run Code Online (Sandbox Code Playgroud)