1

I have some JSON like so:

{"result":[{"job":{"type":"Employee","title":"","occupation":"Underwater Basket Weaver"}}]}

I am getting the occupation value like so:

import org.json.JSONArray;
import org.json.JSONObject;

String occupation = null;

JSONArray resultArray = obj.getJSONArray("result");
JSONObject firstResult = resultArray.getJSONObject(0);
occupation = firstResult.getJSONObject("job").getString("occupation");

However, for some reason, the occupation value is not always a String. My guess is that is could be an int or it could be null. I end up with an exception like this:

org.json.JSONException: JSONObject["occupation"] not a string. at org.json.JSONObject.getString(JSONObject.java:658)

What should you do when you are dealing with JSONObjects that take on variable data types?

1
  • You might have to test if getString("occupation") is valid with isNull(String). According to json.org/javadoc/org/json/… , getString(...) throws the exception if it doesn't find one Commented Jul 8, 2015 at 20:32

4 Answers 4

1

Apply this to receive all the data types in string format

occupation = firstResult.getJSONObject("job").get("occupation").toString();
Sign up to request clarification or add additional context in comments.

2 Comments

This will have NullPointerException if firstResult.getJSONObject("job").get("occupation") returns null.
So, you do this boldface thing all the time, then?
1

This is your initial Json:

{"result":[{"job":{"type":"Employee","title":"","occupation":"Underwater Basket Weaver"}}]}

when you do this:

JSONArray resultArray = obj.getJSONArray("result");

You get:

[{"job":{"type":"Employee","title":"","occupation":"Underwater Basket Weaver"}}]

Then:

JSONObject firstResult = resultArray.getJSONObject(0);

You get:

{"job":{"type":"Employee","title":"","occupation":"Underwater Basket Weaver"}}

Next:

firstResult.getJSONObject("job")

Produce:

{"type":"Employee","title":"","occupation":"Underwater Basket Weaver"}

To finish, is you wanna get the occupation value use: get("occupation").

1 Comment

Your answer reminded me the video on "if google was a guy" when he was shouting "That is not what she asked for".
1

You could do

String occupation = String.valueOf(firstResult.getJSONObject("job").get("occupation"));

if the field occupation always has a value(not array or another JSON object).

Comments

0

I had your problem as well, when you mentioned:

However, for some reason, the occupation value is not always a String. My guess is that is could be an int or it could be null. I end up with an exception like this:

Answer: When your JSON column data type isnt Integer,String... it needs to be defined as an Object or a Class

Object o = new Object();

and then you can use the if-else conditions to check what the wrapper class(your type) can be and then do the following accordingly. Check the image below please.

enter image description here

source of image : https://www.webucator.com/how-to/how-check-object-type-java.cfm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.