Java string to JSON and vice versa

user1281146 picture user1281146 · Mar 21, 2012 · Viewed 8.6k times · Source

I am looking for a way to convert a String to a JSON object and vice versa.

I have a String with JSON notation as value. I need to convert this value to JSON object in order to get specific data out of the JSON objects. Afterwarts, i need to convert a specific JSON object (attribute) to an JAVA attribute.

Does anyone know how to achieve this? I know Java has the import org.json.JSONObject; which enables me to create JSONObjects, but i cannot figure out how to convert my JSON String to a JSON object.

Answer

Gary picture Gary · Mar 21, 2012

You can use the JSONObject constructor

public JSONObject(java.lang.String source) throws JSONException

http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29