Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

java.lang.ClassCastException: java.lang.String cannot be cast to net.minidev.json.JSONObject

    JSONParser jsonParser = new JSONParser();
    Object obj = jsonParser.parse(new FileReader(location.getAbsolutePath()));
    JSONObject jobj = (JSONObject) obj;
share|improve this question
    
it may be relate to your jar or dependency.I think your import should be like this import com.google.gson.Gson; after adding jar. – Anptk Apr 25 '15 at 7:53
up vote 1 down vote accepted

How does your input file look like?

If it's content is something like:

"some string"

then the call to jsonParser.parse() will return java.lang.String causing the subsequent failure of the cast to JSONObject.

share|improve this answer
    
The content is a file from s3.amazonaws.com/Minecraft.Download/indexes/1.7.4.json – Neil D'souza Apr 25 '15 at 7:51
    
I just tried the following code on the file you linked: JSONParser jsonParser = new JSONParser(); Object obj = jsonParser.parse(new FileReader("1.7.4.json")); JSONObject jobj = (JSONObject) obj; System.out.println(((JSONObject) jobj.get("objects")).get("icons/icon_16x16.png")); and I got the following output: {"size":3665,"hash":"bdf48ef6b5d0d23bbb02e17d04865216179f510‌​a"}. are you sure you're still seeing that class-cast-exception? – Itay Maman Apr 25 '15 at 8:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.