Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON String to Dictionary in Android

Tags:

android

I have the following json formatted string that is returned from the web service:

{"Success":false,"Message":"This version is not supported"}

I am using the following code to invoke the web service:

AsyncHttpClient client = new AsyncHttpClient();

client.get("http://mywebsite/check/getcompatibilityinfo", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
        System.out.println(response);
    }
});

The response contains the json string now I need to access the Success and the Message property. Is there any simple way to do it without using complicated third party libraries?

like image 492
john doe Avatar asked Jun 01 '26 09:06

john doe


1 Answers

The JSONObject class is already available in your Android codebase (no 3rd party dependencies). Since your example uses normal (simple) JSON, you can use:

try {
    JSONObject responseJSON = new JSONObject(response);
} catch (JSONException e) {
    e.printStackTrace();
}

boolean success = responseJSON.getBoolean("Success");
String message = responseJSON.getString("Message");
like image 144
Melquiades Avatar answered Jun 04 '26 11:06

Melquiades



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!