错误:org.json.JSONArray无法转换为JSONObject



我一直收到这个错误。我得到错误JSONArray无法转换为JSONObject。我已经解析了包含对象数组的JSON文件,但仍然得到了这个错误。这是代码:

JAVA代码:

public class DetailActivity extends AppCompatActivity {
ImageView imageView;
TextView options, bio, noBio;
private final String JSON_URL = "https://run.mocky.io/v3/987b97bd-6895-40d1-9d37-ce404162c491";
private JsonObjectRequest jsonObjectRequest;
private RequestQueue requestQueue;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
imageView = (ImageView) findViewById(R.id.iv_image);
options = (TextView) findViewById(R.id.tv_options);
bio = (TextView) findViewById(R.id.tv_bio);
noBio = (TextView) findViewById(R.id.tv_no_bio);
getDetails();
}
private void getDetails() {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSON_URL,null ,new Response.Listener<JSONObject>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = new JSONArray(response.toString());
for (int i = 0; i<jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String type = jsonObject.getString("type");
boolean isVisible = jsonObject.getBoolean("isVisible");
if (!isVisible){
imageView.setVisibility(View.GONE);
options.setText("API response is false. So data will not be shown.");
bio.setText("API response is false. So data will not be shown.");
}
else{
imageView.setVisibility(View.VISIBLE);
options.setVisibility(View.VISIBLE);
bio.setVisibility(View.VISIBLE);
Intent intent = getIntent();
Glide.with(getApplicationContext()).load((Bitmap) intent.getParcelableExtra("image")).into(imageView);
options.setText("Choosen Option : " + getIntent().getStringExtra("options"));
String str_bio = getIntent().getStringExtra("comments");
if (str_bio == null){
bio.setVisibility(View.GONE);
noBio.setVisibility(View.VISIBLE);
}
else{
bio.setText("Bio : " + str_bio);
noBio.setVisibility(View.GONE);
}
}
}
} catch (JSONException e) {
Toast.makeText(DetailActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("Exception", e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(DetailActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("Error", error.getMessage());
}
});
requestQueue = Volley.newRequestQueue(DetailActivity.this);
requestQueue.add(jsonObjectRequest);
}

}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0E6FD"
android:orientation="vertical"
tools:context=".DetailActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp">
<ImageView
android:id="@+id/iv_image"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tv_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choosen option : "
android:textSize="17dp"
android:fontFamily="@font/notoserif_bold"
android:layout_marginTop="15dp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/tv_bio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bio :"
android:textSize="17dp"
android:fontFamily="@font/notoserif_bold"
android:layout_marginTop="15dp"
android:textColor="@color/black"/>
<TextView
android:id="@+id/tv_no_bio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Bio provided"
android:gravity="center"
android:textSize="15dp"
android:fontFamily="@font/notoserif_regular"
android:layout_marginTop="15dp"
android:textColor="@color/black"/>
</LinearLayout>

这是JSON文件:

[
{
"type":"PHOTO",
"isVisible":true
},
{
"type":"SINGLE_CHOICE",
"isVisible":true
},
{
"type":"COMMENT",
"isVisible":true
}
]

编写这样的代码来提取JSON:

从服务器获得响应后的Java文件代码:

String json_string = "[ {type:PHOTO, isVisible:true }, { type:SINGLE_CHOICE, isVisible:true }, { type:COMMENT, isVisible:true } ]";
try {
JSONArray jsonArray = new JSONArray(json_string);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
String type = obj.getString("type");
boolean isVisible = obj.getBoolean("isVisible");
Log.e("json", "type ::" + type);
Log.e("json", "isVisible ::" + isVisible);
}

} catch (JSONException e) {
Log.e("Json error", "onCreate:" + e.getMessage());
}

结果:

2020-11-28 11:50:08.806 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: type ::PHOTO
2020-11-28 11:50:08.806 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: isVisible ::true
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: type ::SINGLE_CHOICE
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: isVisible ::true
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: type ::COMMENT
2020-11-28 11:50:08.807 22296-22296/com.googlehomeapp.ansstackoverflow01 E/json: isVisible ::true

最新更新