9

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}"
mooncat39
  • 61
  • 11
Roshan Wijesena
  • 3,106
  • 8
  • 38
  • 57
  • 1
    Maybe it's because s was in a wrong format? Or do you get a different error in that case? In your second example, try to put in an invalid JSON string, like "test123" and see the error. – Philipp Murry Aug 29 '14 at 11:08
  • 1
    Could you change and debug it with Object obj = new JSONParser().parse(s) instead of JSONObject json = (JSONObject)new JSONParser().parse(s) – Devrim Aug 29 '14 at 11:29
  • @aegean yes that is wroking.. thanks i just wondering how could I get print the json elements with it? – Roshan Wijesena Aug 29 '14 at 11:36
  • @PhilippMurry when i am giving "test123" its giving Unexpected character (t) at position 0. – Roshan Wijesena Aug 29 '14 at 11:48

4 Answers4

7

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"}
mooncat39
  • 61
  • 11
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • thanks .. this is working but my problem is when i invoke from args[0].toString(); then not working.. – Roshan Wijesena Aug 29 '14 at 11:30
  • 1
    @RoshanWijesena - Print out what you get from args[0]. You SHOULD NOT see any escapes. Presumably `parse` returns a String error message (which you should test with `instanceof`) when it encounters an error. – Hot Licks Aug 29 '14 at 11:59
  • @HotLicks thanks. When i am print it out it is giving me "{\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}" – Roshan Wijesena Aug 29 '14 at 12:02
  • 1
    @RoshanWijesena answer edited, as per your requirement – Ankur Singhal Aug 29 '14 at 12:04
  • 1
    Yep, the escapes do not belong there. It isn't a C string, so escapes may not be required (depending on the OS and command analyzer used). – Hot Licks Aug 29 '14 at 12:05
  • +1 guys, that could be the reason.. any idea how to remove those escape characters programmatic ally ? – Roshan Wijesena Aug 29 '14 at 12:34
2

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();
}
TanvirChowdhury
  • 2,498
  • 23
  • 28
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
1

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

0

Try this

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 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}

The out put is

{"validityTime":"3600000","callbackUrl":"qwerewqr;ewqrwerq;qwerqwerq","application":"admin","retryAfterFailure":true,"authorizedDomains":"ALL","keytype":"PRODUCTION"}
Shailendra
  • 8,874
  • 2
  • 28
  • 37