0

I am creating JSON using JavaScript as:

var myJson = '{'
    + '"name" : "myname",'
    + '"gender" :"' + gender
    + '"}';

However, while parsing it in Java using:

JSONParser jParser = new JSONParser();
            JSONObject jObj = (JSONObject) jParser.parse(myjson);

I am getting below error:

 (java.lang.ClassCastException) java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONObject
Aquarius24
  • 1,806
  • 6
  • 33
  • 61
  • Which library are you using JSONParser from? also, look at the signature of JSONParser.parse() method, most probably it is returning string. For other options, refer to the question: http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – dubes Sep 08 '16 at 06:50

1 Answers1

0

by using a library, you can do it directly like this:

 JSONObject obj = new JSONObject(stringValue);

you can then iterate over the object.

https://github.com/stleary/JSON-java and for more details you can have a look at Parsing JSON Object in Java

Community
  • 1
  • 1
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • I am using import org.json.simple.JSONObject;, so this won't work – Aquarius24 Sep 08 '16 at 06:46
  • yes, you need the api that does the parsing thing for you. You can check this out too --> http://stackoverflow.com/questions/1395551/convert-a-json-string-to-object-in-java-me – Danyal Sandeelo Sep 08 '16 at 06:48