How to Check the Type of a Value from a JSONobject

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



Leave a reply



Submit