Java.lang.String cannot be passed to org.json.simple.JSONObject simple-json

I get a weird problem trying to parse simple json using google simple-json.

Here is my code that does not work:

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

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

But when I hard code json directly, as below its working capacity. 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 type s, it will give me the following result:

"{\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}"
+4
source share
2 answers

I led this through an eclipse by providing argumentsat 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"}
+5
source

try it

package com.test;

import org.json.simple.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 = (JSONObject) new JSONParser().parse(s);
                System.out.println(json);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

Then on the command line

java -classpath  ".;json-simple-1.1.1.jar" com.test.JSONTest {\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}

Output out

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

All Articles