Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.String cannot be cast to org.json.simple.JSONObject simple-json

I am getting strange problem while trying to parse a simple json using simple-json by google.

Here is my code which is not working:

String s = args[0].toString();
JSONObject json = (JSONObject)new JSONParser().parse(s);

When I execute, it will give me the exception java.lang.String cannot be cast to org.json.simple.JSONObject

But when I hard code json directly like below its working fine. Wat could be the reason?

JSONObject json = (JSONObject)new JSONParser().parse("{\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}");

UPDATE

When I print s, it will give me the output below:

"{\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}"
like image 544
Roshan Wijesena Avatar asked Aug 29 '14 11:08

Roshan Wijesena


3 Answers

I ran this through eclipse by providing arguments in run configuration.

 public static void main(String[] args) {
        String s = args[0].toString();
        System.out.println("=>" + s);
        try {
            JSONObject json = (JSONObject) new JSONParser().parse(s);
            System.out.println(json);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

Output

=>{"application":"admin","keytype":"PRODUCTION","callbackUrl":"qwerewqr;ewqrwerq;qwerqwerq","authorizedDomains":"ALL","validityTime":"3600000","retryAfterFailure":true}


{"validityTime":"3600000","callbackUrl":"qwerewqr;ewqrwerq;qwerqwerq","application":"admin","retryAfterFailure":true,"authorizedDomains":"ALL","keytype":"PRODUCTION"}
like image 50
Ankur Singhal Avatar answered Nov 10 '22 22:11

Ankur Singhal


Make sure that the string is a valid JSON. You can user JSONObject parameterized constructor with the given string to convert the JSON string to a valid JSON object.

For example,

try {
    String jsonString = " {'application':'admin','keytype':'PRODUCTION','callbackUrl':'qwerewqr;ewqrwerq;qwerqwerq','authorizedDomains':'ALL','validityTime':3600000,'retryAfterFailure':true}";
 
    JSONObject data = new JSONObject(jsonString);
    String application = data.getString("application"); //gives admin
    String keytype = data.getString("keytype"); //gives PRODUCTION
} catch (JSONException e) {
    e.printStackTrace();
}
like image 2
Codemaker Avatar answered Nov 10 '22 22:11

Codemaker


I had the same issue


   package com.test;

   import org.json.JSONObject;
   import org.json.simple.parser.JSONParser;
   import org.json.simple.parser.ParseException;

   public class JSONTest {    

        public static void main(String[] args) {

            String s = args[0];
            try {
                JSONObject json = new JSONObject((String) new JSONParser().parse(s));
                System.out.println(json);
            } catch (ParseException e) {
                e.printStackTrace();
            }
      }
   }

This worked for me

like image 1
Mohammad Noman Avatar answered Nov 10 '22 20:11

Mohammad Noman