首页 \ 问答 \ 确定Java中通用对象的类型(Determine type of generic object in Java)

确定Java中通用对象的类型(Determine type of generic object in Java)

我正在处理一些JSON数据,我不知道会收到什么。 例如,对于键“TestKey”,我可能得到“MyValue”的值,就像我得到的值12345或10/10/2005一样。

在Java中是否有一种简单的方法来确定通常归类为“对象”的东西的类型?

我只是希望能够将它分成int,double,date,string等组。而且我确信有一种更为分类的方法,而不是使用正则表达式来查看它。

谢谢


I'm processing some JSON data, and I don't know what I will receive. For example, for key "TestKey" I might get a value of "MyValue" just as likely as I will get a value of 12345 or 10/10/2005.

Is there an easy way in Java to determine the type of something generically classed as "Object"?

I just want to be able to split it up into groups like int, double, date, string, etc. And I'm sure there is a classier way to do it than look at it with regular expressions.

Thanks


原文:https://stackoverflow.com/questions/19649225
更新时间:2022-12-23 15:12

最满意答案

@submission_asset = @submission.attachments.build(submission_asset_params)

更新:

class Submission < ActiveRecord::Base
  has_many :attachments, as: :attachable, inverse_of: :attachable, dependent: :destroy
end

class Attachment < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true

  validates :attachable, presence: true
end

@submission_asset = @submission.attachments.build(submission_asset_params)

UPDATE:

class Submission < ActiveRecord::Base
  has_many :attachments, as: :attachable, inverse_of: :attachable, dependent: :destroy
end

class Attachment < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true

  validates :attachable, presence: true
end

相关问答

更多
  • 这是回调链中的“赛车状况”。 创建产品时,它在保存之前没有任何标识,因此ProductCategory的范围内没有product 。 Product.new(name: "modern times", category_ids:[1, 2]) #=> # 在验证阶段(保存前), ProductCatgory不能为其外部product_id分配任何id。 这就是你有关联验证的原因:所以验证发生在整个交易的范围内 更新:正如评论中所说,你仍然无法确保产品/类别的存在。 有很 ...
  • 这是我的建议。 我在sqlite3(带有内存数据库)中这样做只是为了演示目的。 连接(在rails中,使用database.yml代替): ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:' 设置类: class Customer < ActiveRecord::Base has_many :book_managers has_many :books, :through => ...
  • 我不认为这可以在has_many调用中实现。 我只是在我的User类中定义一个实例方法来获得该功能: def bookmarks_for_category(category) bookmarks.includes(:objects).where('objects.category = ?', category) end 不确定这个方法的实现,但重点是我认为你不能有一个接受参数的has_many 。 您可以研究的另一件事是范围 。 在这种情况下,您可能希望在Bookmark模型上定义for_catego ...
  • @submission_asset = @submission.attachments.build(submission_asset_params) 更新: class Submission < ActiveRecord::Base has_many :attachments, as: :attachable, inverse_of: :attachable, dependent: :destroy end class Attachment < ActiveRecord::Base belong ...
  • 事实证明这是一个问题。 https://github.com/activerecord-hackery/squeel/issues/232 Turns out it is a problem with squeel. https://github.com/activerecord-hackery/squeel/issues/232
  • 这只意味着其中一个利益在另一端没有相关的职位。 很可能它被删除了。 这可以通过以下方式防止: class Post < ActiveRecord::Base has_many :interests, :dependent => :destroy end 同时你应该清理数据库中的孤儿。 编辑:你声称这已经在你的模型中,但如果是这样,那么你不清楚如何可以有一个孤儿的兴趣,如错误所示。 也许它是在添加依赖子句之前创建的? 再次,通过SQL删除孤儿,然后再试一次。 如果稍后问题重新出现,则必须在不带回调的情况 ...
  • @account = Account.new(params[:user]) @accountgame = @account.account_games.build(:game => Game.first, :score => 100) @accountgame.save 尽管我强烈建议如果你开始向你的连接模型添加列,你可以将它称为不同的例如“订阅”或“会员”或类似的东西。 一旦你添加了列,它就不再是一个连接模型,而只是一个常规模型。 @account = Account.new(params[:user] ...
  • 我想你拼错了你的模特。 你称之为患者,但我认为你的意思是Pupil。 此外,最好在迁移中使用t.references而不是belongs_to。 但看起来你到目前为止表现还不错。 您可能会犯其他一些错误,但我会修复它,看看还有什么问题。 I think you misspelled your model. You're calling it Patient but I think you meant Pupil. Also, it's better to user t.references rather t ...
  • 您应该在组和用户之间有两个关系。 一个反映用户创建组的事实,以及用户属于组的事实。 您可以通过配置关系的命名来反映这一想法。 您还必须将“user_id”字段添加到“组”表中。 class User < ApplicationRecord has_many :created_groups, class_name: "Group" has_many :memberships has_many :groups, through: :memberships end class Group < App ...
  • 首先,您可以使用它来设置@product的industry_ids来修复您的第一个集合选择: <%= form_for @product do |f| %> <%= f.collection_select :industry_ids, Industry.all, :id, :name %> <% end %> 这将允许您设置collection_singular_ids方法,该方法适用于所有has_many关联。 你必须用params方法备份它: #app/controllers/products_c ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)