活动模型序列化程序格式化 JSON 显示
Active Model Serializer formatting JSON display
我在我的 rails 应用程序中使用活动模型序列化器,我想重构显示:
每当我去 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"
}]
}
}
}
}
我花了很多时间浏览 adapters, rendering, architecture,但找不到指南。首先,是否可以让它看起来像上面的第二个代码块?第二,如果可以的话,我必须怎么做才能改变显示?
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
不使用浏览器,我建议使用 Postman
pretty_generate
.
您可以使用以下代码使用缩进和换行符呈现它:
<pre><%= JSON.pretty_generate(your_json_here) %></pre>
在您上面的代码中使用:
<%
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>
您的问题是关于 json 输出的样式。正如@araratan 上面所建议的那样,您可以使用它来只为您提供可读性。并且没有必要在机器上使用时尚。
如果你需要在 thGenerate preety json outpur
中看到相同的外观
我在我的 rails 应用程序中使用活动模型序列化器,我想重构显示:
每当我去 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"
}]
}
}
}
}
我花了很多时间浏览 adapters, rendering, architecture,但找不到指南。首先,是否可以让它看起来像上面的第二个代码块?第二,如果可以的话,我必须怎么做才能改变显示?
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
不使用浏览器,我建议使用 Postman
pretty_generate
.
您可以使用以下代码使用缩进和换行符呈现它:
<pre><%= JSON.pretty_generate(your_json_here) %></pre>
在您上面的代码中使用:
<%
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>
您的问题是关于 json 输出的样式。正如@araratan 上面所建议的那样,您可以使用它来只为您提供可读性。并且没有必要在机器上使用时尚。 如果你需要在 thGenerate preety json outpur
中看到相同的外观