如何将解析后的JSON作为JSON::Any类型映射到自定义对象?
就我而言,我正在研究聊天客户端.Chat API可以使用以下JSON响应请求:
{"ok" => true,
"result" =>
[{"update_id" => 71058322,
"message" =>
{"message_id" => 20,
"from" => "Benjamin",
"text" => "hey"}}]}
Run Code Online (Sandbox Code Playgroud)
在我的API客户端代码中的某处,我解析这些JSON以执行一些基本的运行状况检查并将结果传递给响应使用者.在使用者中,我迭代result数组并尝试将每个更新转换为适当的对象:
module Types
class Update
JSON.mapping({
update_id: {type: Int32},
message: {type: Message},
})
end
end
module Types
class Message
JSON.mapping({
message_id: Int32,
date: Int32,
text: String,
})
end
end
return unless response["ok"]
response["result"].each do |data|
update = Types::Update.from_json(data)
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,最后一行会导致编译错误:
no overload matches 'JSON::Lexer.new' with type JSON::Any
Run Code Online (Sandbox Code Playgroud)
显然,Object.from_json只能接受StringJSON,但不能接受解析的JSON.在我的情况下data是JSON::Any对象.
肮脏的修复Types::Update.from_json(data.to_json)工作,但它看起来很荒谬.
将JSON对象映射到保留所有嵌套结构的自定义类型的正确方法是什么?
JSON.mapping与...无法很好地协同工作JSON.parse.要解决您的问题,您可以创建另一个映射Types::Result并解析一个孔json,使用Object.from_json它更方便使用:
module Types
class Message
JSON.mapping(
message_id: Int32,
text: String
)
end
class Update
JSON.mapping(
update_id: Int32,
message: Message
)
end
class Result
JSON.mapping(
success: { key: "ok", type: Bool },
updates: { key: "result", type: Array(Update) }
)
end
end
result = Types::Result.from_json string_json
result.success # => true
result.updates.first.message.text # => "hey"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
553 次 |
| 最近记录: |