Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

android

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.

like image 479
Mick Avatar asked Feb 08 '13 12:02

Mick


2 Answers

You have imported the wrong class. Change

import org.json.JSONObject;

to

import org.json.simple.JSONObject;
like image 200
Henry Avatar answered Nov 10 '22 11:11

Henry


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

like image 36
ρяσѕρєя K Avatar answered Nov 10 '22 11:11

ρяσѕρєя K