Question

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

Était-ce utile?

La solution

You can't, your best choice is pass an extra field with some kind of data to identify the field type. But that would be dirty. The best option is always be sure of what kind of field will you get in the JSON, if you achieve that, then you will get compatibility with the JSON libraries like Gson. If you don't, you are by your own and you will have to pass some kind of field like type:0 (where 0 is for int, 1 for double and so on...) and make a dirty switch to cast the field properly.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top