How to check the type of a value from a JSONObject?
You can get the object from the JSON with the help of JSONObject.get()
method and then using the instanceof
operator to check for the type of Object.
Something on these lines:-
String jString = "{\"a\": 1, \"b\": \"str\"}";
JSONObject jObj = new JSONObject(jString);
Object aObj = jObj.get("a");
if (aObj instanceof Integer) {
// do what you want
}
JSONObject - How to get a value?
String loudScreaming = json.getJSONObject("LabelData").getString("slogan");
how to check what return type is of object in jsonobject
Replace
if (VODB.getJSONArray("KNDV430") instanceof JSONArray)
with
if (VODB.get("KNDV430") instanceof JSONArray)
Java - Retrieve values of variable type from JSONObject
Apply this to receive all the data types in string format
occupation = firstResult.getJSONObject("job").get("occupation").toString();
How can the datatype of a JSON property be determined?
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");
}
how to check if all JSON values are of type string
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
if (jsonObject.get(key) instanceof String) {
// do something with jsonObject here
}
}
How to check for json date type of a value from a JSONObject?
Json structures don't hold Date types. You need to convert it yourself.
You could do:
else if(aObj instanceof String) {
if (aObj.toString().startsWith("/Date")) {
System.out.println(aObj+" is Date");
}
else
System.out.println(aObj+" is String");
then later
String str = "/Date(1463667774000-9000)/";
Date date = new Date(Long.parseLong(str.replaceAll(".*?(\\d+).*", "$1")));
Related Topics
How to Take Screenshots Fast in Java
Why Do Constructors in Java Not Have a Return Type
What Is the Java Equivalent of Sscanf for Parsing Values from a String Using a Known Pattern
Is There a Commonly Used Rational Numbers Library in Java
Mockito: Mock Private Field Initialization
How to Add New Methods to the String Class in Java
Post Increment Operator Not Incrementing in for Loop
Postgresql Uuid Supported by Hibernate
Differences Between Runtime/Checked/Unchecked/Error/Exception
Dynamically Adding Items to a Jcombobox
JPA SQL Server No Dialect Mapping for Jdbc Type: -9
How to Change Java Logging Console Output from Std Err to Std Out
Java Type Generic as Argument for Gson
How to Create a Stream of Regex Matches