Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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":"bdf48ef6b5d0d23bbb02e17d04865216179f510a"}. 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.