错误:org.json.jsonarray无法转换为jsonobject

rdrgkggo  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(328)

这个问题在这里已经有答案了

org.json.jsonarray无法转换为jsonobject(5个答案)
上个月关门了。
我一直在犯这个错误。我得到了错误 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
   }
]
tvokkenx

tvokkenx1#

编写这样的代码来提取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

相关问题