-2

I am facing - java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.JSONObject error while casting org.json.simple.JSONObject to org.json.JSONObject, I am not using JSONArray as it is suggested in one of the question asked earlier. below is my code, I am trying to assert JSONObject from API to JSONObject from a json text file. The exception is coming on "jsonObject = (JSONObject) obj;" line number 42.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.xxxxxx.glue.resin.Resin;
import com.xxxxxx.glue.resin.ResinDataFactory;
import com.xxxxxx.glue.resin.ResinResponse;

 class OLUtil {

 public static JSONObject  getJsonForOLResponse(String uri){
        JSONObject jsonObj = null;
        try {
            Resin resinAPI = ResinDataFactory.getResinAPI(null, null, null, null, null, null, uri, null);
            ResinResponse apiResponse = resinAPI.resinGet();
             jsonObj = apiResponse.getJsonResponse();
        } catch(Exception e ){
            e.printStackTrace();
        }
        return jsonObj;
    }

 public static JSONObject getJsonForOLTestData(File expectedDataJsonDataPath){
     FileReader reader = null;
     JSONObject jsonObject = null;
    try {
        reader = new FileReader(expectedDataJsonDataPath);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     JSONParser jsonParser = new JSONParser();
     try {
        Object obj = jsonParser.parse(reader);
        jsonObject = (JSONObject) obj;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     return jsonObject;
    }
}
Shek
  • 1,543
  • 6
  • 16
  • 34
  • 4
    You're using a `org.json.simple.parser.JSONParser` to parse the JSON, and expecting to get a `org.json.JSONObject`, but actually you get a `org.json.simple.JSONObject` - which is what that parser returns. Just change the import of `JSONObject` to the `simple` version? Or use a different parser? – Andy Turner Jan 25 '16 at 17:21
  • I am casting it to org.json.JSONObject. – Shek Jan 25 '16 at 17:22
  • [`org.json.JSONObject`](http://www.json.org/javadoc/org/json/JSONObject.html) and [`org.json.simple.JSONObject`](http://juliusdavies.ca/json-simple-1.1.1-javadocs/org/json/simple/JSONObject.html) are unrelated types. You can't cast between arbitrary types. – Andy Turner Jan 25 '16 at 17:23
  • Got it, then there is no way to cast, I have to change the code of parsing json text file?. thanks – Shek Jan 25 '16 at 17:25
  • what's up with the people now'r days, can't stand a honest mistake, thanks for the down vote LOL – Shek Jan 25 '16 at 17:30
  • Well, I haven't downvoted; but consider that one reason in the tooltip for downvoting is "This question does not show any research effort." You could have found the Javadoc for these two classes trivially to discover they are unrelated; even if you didn't know already that unrelated classes cannot be cast, you could also have found http://stackoverflow.com/questions/907360/can-someone-explain-classcastexception-in-java. – Andy Turner Jan 25 '16 at 17:31
  • I researched on stackoverflow and one of the question had same exception and they were asked to use JSONArray and yes I should've checked java docs first my mistake and I admit it. – Shek Jan 25 '16 at 17:35

1 Answers1

1

you parsing it from different parser. and assigning to other type, where even casting also won't work out.

  • thanks, yup, I found my mistake, the functions I was using from legacy code were of org.json.JSONObject type and I mistook it as same type and tried to cast. – Shek Jan 25 '16 at 17:33