I am using org.json.simple.JSONObject.
I want to convert string to Json object.
String value=request.getParameter("savepos");
JSONObject jsonObject = (JSONObject) JSONValue.parse(value);
It doesn't work. Why?
Olivier Pons
15.9k27 gold badges123 silver badges225 bronze badges
asked Jun 10, 2015 at 13:00
Talib
1,1648 gold badges31 silver badges62 bronze badges
4 Answers 4
Try this:
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
Seki
11.5k7 gold badges50 silver badges75 bronze badges
answered Jun 10, 2015 at 13:07
Shailesh Yadav
1,1312 gold badges15 silver badges30 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Menuka Ishan
Since There are Multiple JSONParser libraries, Library name would be helpful
Ghost Rider
how to convert from Java object ( Sample s = new Sample()) to JSON string?
Mukit09
here json.simple Parser is used, I guess.
ado387
@MenukaIshan You can import json-simple's parser from
org.json.simple.parser.JSONParserbarrypicker
Why are all the examples so limited and incomplete? No import namespace included, the method seems to require a try-catch block, no example how to transfer back to string... Thanks for this idea, but it could be so much more helpful if you simply completed the thought...
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(value);
should do the work.
answered Jun 10, 2015 at 13:08
Arthur Eirich
3,6789 gold badges37 silver badges68 bronze badges
Comments
Converting String to Json Object by using org.json.simple.JSONObject
private static JSONObject createJSONObject(String jsonString){
JSONObject jsonObject=new JSONObject();
JSONParser jsonParser=new JSONParser();
if ((jsonString != null) && !(jsonString.isEmpty())) {
try {
jsonObject=(JSONObject) jsonParser.parse(jsonString);
} catch (org.json.simple.parser.ParseException e) {
e.printStackTrace();
}
}
return jsonObject;
}
answered Dec 4, 2019 at 12:13
Anzar Ansari
1091 silver badge5 bronze badges
Comments
In the newer com.github.cliftonlabs.json_simple the code is the following:
JsonObject obj = Jsoner.deserialize(responseString, new JsonObject());
As documented on the project's API docs
answered Jan 6, 2021 at 19:40
Kartik Chugh
1,1531 gold badge16 silver badges31 bronze badges
Comments
lang-java
value? What happens with the code you've tried?