1

Casting Map to a JSONObject for android app. Builds but crashes on run-time. Looked in the Logcat and got error:

org.json.JSONObject cannot be cast to java.util.Map

Here is the section related:

JSONObject item = new JSONObject(data);
Map product = ((Map)item.get("product"));

It's specifically the second line that's making it crash. I commented out code until un-commenting this line caused the crash.

The JSON it's linked to is here.

Unmapping the JSONObject gives this error:

Incompatible types.

Required: java.util.Map<, >

Found: java.lang.Object

More extensive code view:

        TextView parsed = findViewById(R.id.jsonParse);
        String barcodeNum = result.getText();
        String productName = "";

        try {
            URL url = new URL("https://world.openfoodfacts.org/api/v0/product/" + barcodeNum + ".json");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String data = "";
            String line = "";

            while (line != null){
                line = bufferedReader.readLine();
                data = data + line;
            }


            JSONObject item = new JSONObject(data);
            final JSONObject product = item.getJSONObject("product");
            final Map<String, Object> map =
                    product.keySet()
                            .stream()
                            .collect(Collectors.toMap(
                                    Function.identity(),
                                    product::get
                            ));
2
  • What is data and why Map? @George Hanlon Commented Mar 29, 2019 at 22:12
  • @MS90 data is a parsed String of the json contents. Commented Mar 29, 2019 at 22:16

1 Answer 1

2
JSONObject#get

isn't going to return a Map. Instead it will return another JSONObject, which describes the nested product property.

You'll see, indeed, it can be casted to it

final JSONObject product = (JSONObject) item.get("product");

What you can do is

final JSONObject product = item.getJSONObject("product");
final Map<String, Object> objectMap = product.toMap();

On old versions of JSON-Java, which don't offer the toMap method, what you can do is

final JSONObject product = item.getJSONObject("product");
final Map<String, Object> map =
        product.keySet()
               .stream()
               .collect(Collectors.toMap(
                       Function.identity(),
                       product::get
               ));
Sign up to request clarification or add additional context in comments.

21 Comments

Thanks for this. Think I'm being stupid here but using .toMap() gives me: Cannot resolve method 'toMap()' Tried looking here: stackoverflow.com/questions/27841225/…
@GeorgeHanlon could you tell me which version of JSON-Java you're using? The toMap method is pretty recent, and you might not be able to use it on old releases.
@GeorgeHanlon updated with a way to obtain a Map when using old versions of JSON-Java.
hm that's strange, the old version of JSON-Java method " Cannot resolve symbol 'jsonObject' ". Same with .keySet(). And yet it doesn't seem to have the .toMap() method. Apologies for being a novice, but how would I check the version of JSON-Java? Thanks for the help - really appreciated.
@GeorgeHanlon can you do a screenshot of the available methods?
|

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.