0

I am trying to recreate a http Post request to consume a webAPI. I originally created it in C# and got it to work. But the java analogous seems to be giving me a bit of ...

my input string consists of parameters expected by the webapi.

       String input = "{" +
                "\"UserID\":xxx," +
                "\"UserPsw\":\"yyy," +
                "\"ApiFunction\":zzz," +
                "\"DppName\":aaaa," +
                "\"DppVersion\":Latest, " +
                "\"ClearData\"ftfdgfdgfdg fdgfdgfd 4354534," +
                "\"ResultType\":JSON \"}";

Now I downloaded the JSON-simple jar file from:- https://code.google.com/archive/p/json-simple/downloads

and imported it in my libraries folder for the java project.

then for my main.java class, I imported the following:-

import org.json.simple.JSONObject;

The intention is to convert my 'input' string into a JSON onbject as such:-

 JSONObject jsonObject = new JSONObject(input);

but I am getting an error that says:-

cannot find symbol
  symbol:   constructor JSONObject(java.lang.String)
  location: class org.json.simple.JSONObject

what am I doing wrong?

The reason I want to convert it into a JSON object, is because my webapi expects a certain format for a string...

{"UserID":xx,"UserPsw":yyy,"ApiFunction":zzz,"DppName":aaaa,"DppVersion":Latest, "ClearData":ftfdgfdgfdg fdgfdgfd 4354534,"ResultType":JSON "}

however my java string input is formatted as:-

{"UserID":"xx","UserPsw":"yyy","ApiFunction":"zzz","DppName":"aaaa","DppVersion":"Latest", "ClearData":"ftfdgfdgfdg fdgfdgf 4354534","ResultType":"JSON"}

which is different than how C# sends it as a class object with quotes only around property names and not around values.

Philo
  • 1,931
  • 12
  • 39
  • 77

1 Answers1

0

According to the docs

http://juliusdavies.ca/json-simple-1.1.1-javadocs/org/json/simple/JSONObject.html

there is no constructor that accepts a String as parameter. You may think about converting it to a Map which would make it clearer and less error-prone.

 HashMap<String,String> newMap = new HashMap<>();
 newMap.put("UserID","xxx");
 //... the rest of your attributes
 JSONObject jsonObject = new JSONObject(newMap);

As proposed by 'Mappan' you could use the JSONParser, as well:

try {
  JSONParser parser = new JSONParser();
  JSONObject json = (JSONObject) parser.parse(stringToParse);
} catch (org.json.simple.parser.ParseException e) {
   e.printStackTrace();
}

Taken from : How to convert String to JSONObject in Java

Community
  • 1
  • 1
Jan B.
  • 6,030
  • 5
  • 32
  • 53
  • I would like to do something like this http://stackoverflow.com/questions/5245840/how-to-convert-string-to-jsonobject-in-java – Philo Jul 29 '16 at 22:42
  • doesn't allow me to format my string as Mappan points out. tried it.. also, look at charu's answer – Philo Jul 29 '16 at 22:46
  • unreported exception org.json.simple.parser.ParseException; must be caught or declared to be thrown... on the second line where JSON object is invoked – Philo Jul 29 '16 at 22:50
  • Ok :-) That's an exception, that you need to catch. I'll edit my post. – Jan B. Jul 29 '16 at 22:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118675/discussion-between-matt-and-philo). – Jan B. Jul 29 '16 at 22:56
  • worked for me... except in simple.parser... ParsingException is not the correct class name, rather it should be ParseException. – Philo Jul 29 '16 at 23:46
  • Excellent. Thanks for the hint, edited and corrected. – Jan B. Jul 30 '16 at 00:20