[Solved] org.json.simple.JSONObject cannot be cast to org.json.JSONObject

When I run the following code…

  JSONObject jsonObject = null;
  JSONParser parser=new JSONParser(); // this needs the "json-simple" library

  try 
  {
        Object obj = parser.parse(responseBody);
        jsonObject=(JSONObject)obj;
  }
  catch(Exception ex)
  {
        Log.v("TEST","Exception1: " + ex.getMessage());
  }

…at runtime I see the following in my log output:

Exception1: org.json.simple.JSONObject cannot be cast to org.json.JSONObject

I don’t understand why.

Enquirer: Mick

||

Solution #1:

You have imported the wrong class. Change

import org.json.JSONObject;

to

import org.json.simple.JSONObject;
Respondent: Henry

Solution #2:

Change your code as:

  org.json.simple.JSONObject jsonObject = null;
  JSONParser parser=new JSONParser(); // this needs the "json-simple" library

  try 
  {
        Object obj = parser.parse(responseBody);
        jsonObject=(org.json.simple.JSONObject)obj;
  }
  catch(Exception ex)
  {
        Log.v("TEST","Exception1: " + ex.getMessage());
  }

or if you are using only org.json.simple library for parsing json string then just import org.json.simple.* instead of org.json.JSONObject

Respondent: ??????? K

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.