title: org.json.JSONObject cannot be cast to java.util.Map
body: <p>Casting Map to a JSONObject for android app. Builds but crashes on run-time. Looked in the Logcat and got error:</p>
  <blockquote>
  <p>org.json.JSONObject cannot be cast to java.util.Map</p>
  </blockquote>
  <p>Here is the section related:</p>
  <pre><code>JSONObject item = new JSONObject(data);
  Map product = ((Map)item.get(&quot;product&quot;));
  </code></pre>
  <p>It's specifically the second line that's making it crash. I commented out code until un-commenting this line caused the crash.</p>
  <p>The JSON it's linked to is <a href="https://world.openfoodfacts.org/api/v0/product/737628064502.json" rel="nofollow noreferrer">here</a>.</p>
  <p>Unmapping the JSONObject gives this error:</p>
  <blockquote>
  <p>Incompatible types.</p>
  <p>Required:  java.util.Map&lt;, &gt;</p>
  <p>Found:     java.lang.Object</p>
  </blockquote>
  <p>More extensive code view:</p>
  <pre><code>        TextView parsed = findViewById(R.id.jsonParse);
          String barcodeNum = result.getText();
          String productName = &quot;&quot;;
  
          try {
              URL url = new URL(&quot;https://world.openfoodfacts.org/api/v0/product/&quot; + barcodeNum + &quot;.json&quot;);
              HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
              InputStream inputStream = httpURLConnection.getInputStream();
              BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
              String data = &quot;&quot;;
              String line = &quot;&quot;;
  
              while (line != null){
                  line = bufferedReader.readLine();
                  data = data + line;
              }
  
  
              JSONObject item = new JSONObject(data);
              final JSONObject product = item.getJSONObject(&quot;product&quot;);
              final Map&lt;String, Object&gt; map =
                      product.keySet()
                              .stream()
                              .collect(Collectors.toMap(
                                      Function.identity(),
                                      product::get
                              ));
  </code></pre>