Android 解析 json 得到 org.json.JSONException 错误

Android parse json get org.json.JSONException error

    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 结果是:

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

不幸的是我得到这个错误是 catch:

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

我的代码有什么问题。在服务器中我只有这个代码:

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

subject 键位于 JSONArray 内的 JSONObject 中,因此从 jsonStr 字符串获取 JSONArray:

JSONArray arrJSON = new JSONArray(jsonStr);
JSONObject object=arrJSON.getJSONObject(0);
Log.e("code:" , object.getString("subject"));