Java 错误:org.json.JSONArray无法转换为JSONObject
我不断地犯这个错误。我发现错误Java 错误:org.json.JSONArray无法转换为JSONObject,java,android,json,android-volley,Java,Android,Json,Android Volley,我不断地犯这个错误。我发现错误JSONArray无法转换为JSONObject。我已经解析了包含对象数组的JSON文件,但是仍然得到了这个错误。代码如下: JAVA代码: public class DetailActivity extends AppCompatActivity { ImageView imageView; TextView options, bio, noBio; private final String JSON_URL = "https://run.mocky.
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);
}
[
{
"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());
}
您能发布logcat tht指示错误吗?logcat中没有显示错误。我只在上面加上了截击错误。哪一行?。在你的帖子上,你有一个log
log.d(“Error”,Error.getMessage())
还可以让您像这样捕获catch(Throwable e)
并像这样打印e.printStackTrace()
您正在生成一个JsonObjectRequest
但是响应是一个json数组,使用JsonArrayRequest
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