I am trying to convert a String to a JSON in Java. I tried Gson and JSONObject.toJson but they are not helping me .
String str = "{"rahul":"reddy"}";
This is my string , I want this to be converted into a JSON.Can anyone please help me .
I am trying to convert a String to a JSON in Java. I tried Gson and JSONObject.toJson but they are not helping me .
String str = "{"rahul":"reddy"}";
This is my string , I want this to be converted into a JSON.Can anyone please help me .
If you're looking to parse JSON from a string to an object, firstly you'll need to choose a library. An easy choice is json-simple
// note that I have escaped the inner "
String string = "{\"rahul\":\"reddy\"}";
JSONObject json = (JSONObject)new JSONParser().parse(string);
To get values back:
json.get("rahul"); // --> "reddy"