4

I am using Gson library to convert Map to JSON. But requirement is to use json-simple-1.1.1. I google for solution but my bad luck couldn't get solution.

I want to use JSON Simple to achieve following operation:

/** Converting Map to JSON and sending response back to view */
private void write(HttpServletResponse response, Map<String, Object> map) throws IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(new Gson().toJson(map)); 
}

any help is appreciated!

4 Answers 4

4

Try to use

//import org.json.simple.JSONValue;

String jsonText = JSONValue.toJSONString(map);
System.out.print(jsonText);
Sign up to request clarification or add additional context in comments.

Comments

2

Try using this :

new JSONObject(map);

1 Comment

I got this error: "The method write(int) in the type PrintWriter is not applicable for the arguments (JSONObject)"
1

Try it, this will work:

org.json.simple.JSONObject jsonObj = new org.json.simple.JSONObject();
String jsonStr = jsonObj.toJSONString(map);

Comments

0

Use this

response.getWriter().write(JSONValue.toJSONString(map));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.