0
    String jsonStr = HelperInputStream.convertInputStreamToString(inputStream);
    if (jsonStr == null) {
        return;
    }
    String code,message = "";
    try {
        JSONObject object = new JSONObject(jsonStr);
        Log.e("code:" , object.getString("subject"));

jsonStr result is:

[{"code":"1","subject":"you have new message"}]

unfortunately i get this error is catch:

org.json.JSONException: Value [{"subject":"you have new message","code":"1"}] of type org.json.JSONArray cannot be converted to JSONObject

whats my code problem. in server i have only this code:

<?php
    echo json_encode(array(
        array(
            'code'=>'1',
            'subject'=>"you have new message",
        )
    ));
?>

1 Answer 1

2

subject key is in JSONObject which is inside JSONArray so get JSONArray from jsonStr string:

JSONArray arrJSON = new JSONArray(jsonStr);
JSONObject object=arrJSON.getJSONObject(0);
Log.e("code:" , object.getString("subject"));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks sir, why i can not use switch for String code = i.getStringExtra("code"); i like to using switch instead of if, i get this error: Error:(145, 24) java: strings in switch are not supported in -source 1.6 (use -source 7 or higher to enable strings in switch)

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.