After decrypt of encoded API response got the json string as follow."{"result":"SUCCESS","ts":"2024-12-12T13:18:07+05:30"}"
. In this above encrypted format is include with starting "
and ending "
character.
Tried following code
JsonParser.parseString()
to parse the above string but not able to parse the JSON string.
1
Using Gson:
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
String jsonResponse = "{"result":"SUCCESS","ts":"2024-12-12T13:18:07+05:30"}";
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
String result = jsonObject.get("result").getAsString();
String timestamp = jsonObject.get("ts").getAsString();
System.out.println("Result: " + result);
System.out.println("Timestamp: " + timestamp);
Add Gson dependency in build.gradle
:
implementation 'com.google.code.gson:gson:2.8.9'
Using JSONObject:
import org.json.JSONObject;
String jsonResponse = "{"result":"SUCCESS","ts":"2024-12-12T13:18:07+05:30"}";
JSONObject jsonObject = new JSONObject(jsonResponse);
String result = jsonObject.getString("result");
String timestamp = jsonObject.getString("ts");
System.out.println("Result: " + result);
System.out.println("Timestamp: " + timestamp);
Both methods handle JSON parsing directly. Use whichever library suits your project.