Org.json.simple.JSONObject cannot be passed 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:

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

      

I do not understand why.

+3


source to share


2 answers


You imported the wrong class. Change

import org.json.JSONObject;

      



to

import org.json.simple.JSONObject;

      

+7


source


Change your code like:

  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 only using a library org.json.simple

to parse the json string, just import org.json.simple.*

insteadorg.json.JSONObject

+1


source







All Articles