How to determine the data type of a JSON property?

In Java, I know that you can check if a key is present using the isNull () method. Is there a way to check what data the key stores?

Consider the following examples.

I need a function like JSONBody.getDataType ("key") and it will return String

{ "key" : "value" } 

I need a function like JSONBody.getDataType ("key"), and it will return JSONObject

 { "key" : { "parm1" : "value1", "parm2" : "value2" } } 

I need a function like JSONBody.getDataType ("key") and it will return a JSONArray

 { "key" : [ "value1", "value2", "value3" ] } 

I need a function like JSONBody.getDataType ("key") and it will return a Boolean

 { "key" : true } 

Is there something similar?

+8
java json
source share
1 answer
 JSONObject stuff = new JSONObject(whatever); Object thing = stuff.get("key"); String classNameOfThing = thing.getClass().getName(); Systen.out.println("thing is a " + classNameOfThing); if (thing instanceof Integer) { System.out.println("thing is an Integer"); } 
+6
source share

All Articles