android 中无法将 JSONObject 转换为 JSONArray

JSONObject cannot be converted to JSONArray in android

我是 android 开发新手。我遇到了这样的错误:- JSON Object cannot be convert in jsonArray:-

我的ProductInner.java:

public class ProductInner extends AppCompatActivity {

TextView textView;
ImageView imageView;
LinearLayout linearLayout;
String s,s1;
List<String> mainImage = new ArrayList<>();
List<String> outerlist = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.product_inner);

    textView = (TextView)findViewById(R.id.product_inner_page_product_name);
    imageView = (ImageView)findViewById(R.id.product_inner_page_product_main_image);
    linearLayout = (LinearLayout)findViewById(R.id.sub_images_show_section);

    new ProductInnerDisplay().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/product&id=80&key=test123$");
}

public class ProductInnerDisplay extends AsyncTask<String,Void,Void>{

    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        dialog = new ProgressDialog(ProductInner.this);
        dialog.show();
    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(params[0]);
            HttpResponse response = client.execute(post);

            int status = response.getStatusLine().getStatusCode();

            if(status==200){

                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsonObject = new JSONObject(data);
               JSONArray jsonArray = jsonObject.getJSONArray("rproducts");
                JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");


                for(int i=0;i<jsonArray.length();i++){

                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    List<String> list = new ArrayList<>();
                    list.add(jsonObject1.getString("product_id"));
                    list.add(jsonObject1.getString("thumb"));
                    list.add(jsonObject1.getString("name"));
                    list.add(jsonObject1.getString("price"));
                    list.add(jsonObject1.getString("rating"));
                    list.add(jsonObject1.getString("reviews"));
                    list.add(jsonObject1.getString("href"));

                    Log.d("json parse","");
                }
                for(int i=0;i<jsonArray1.length();i++){
                    Log.d("length of",jsonArray1.length()+"");

                    JSONObject jsonObject1 = jsonArray1.getJSONObject(i);

                    List<String> list = new ArrayList<>();

                    list.add(jsonObject1.getString("id"));
                  //  list.add(jsonObject1.getString("name"));
                    s=jsonObject1.getString("name");
                    list.add(jsonObject1.getString("model"));
                    list.add(jsonObject1.getString("reward"));
                    list.add(jsonObject1.getString("points"));
             //       list.add(jsonObject1.getString("image"));
                    outerlist.add(jsonObject1.getString("image"));
                    JSONArray jsonArray2 = jsonObject1.getJSONArray("images");

                    for(int j=0;j<jsonArray2.length();j++){

                        List<String> list1 = new ArrayList<>();
                        Log.d("i am here","");
                        mainImage.add(jsonArray2.getString(j));
                        Log.d("i am here next", "");

                       // list1.add(jsonArray2.getString(j));
                    }
                    list.add(jsonObject1.getString("price"));
                    Log.d("i am here 1", "");

                    JSONArray jsonArray3 = jsonObject1.getJSONArray("options");

                    for(int j=0;j<jsonArray3.length();j++){

                        JSONObject jsonObject2 = jsonArray3.getJSONObject(j);

                        List<String> list1 = new ArrayList<>();

                        list1.add(jsonObject2.getString("product_option_id"));
                        list1.add(jsonObject2.getString("option_id"));
                        list1.add(jsonObject2.getString("name"));
                        list1.add(jsonObject2.getString("type"));

                        JSONArray jsonArray4 = jsonObject2.getJSONArray("option_value");

                        for(int k=0;k<jsonArray4.length();k++){

                            JSONObject jsonObject3 = jsonArray4.getJSONObject(k);

                            List<String> list2 = new ArrayList<>();

                            list2.add(jsonObject3.getString("product_option_value_id"));
                            list2.add(jsonObject3.getString("option_value_id"));
                            list2.add(jsonObject3.getString("name"));
                            list2.add(jsonObject3.getString("image"));
                            list2.add(jsonObject3.getString("price"));
                            list2.add(jsonObject3.getString("price_prefix"));
                        }
                        list1.add(jsonObject2.getString("required"));
                    }
                    list.add(jsonObject1.getString("minimum"));
                    list.add(jsonObject1.getString("newprice"));
                    list.add(jsonObject1.getString("qty"));
                    list.add(jsonObject1.getString("rating"));
                    list.add(jsonObject1.getString("description"));

                    JSONArray jsonArray4 = jsonObject1.getJSONArray("attribute_groups");

                    for(int j=0;j<jsonArray4.length();j++){

                        JSONObject jsonObject2 = jsonArray4.getJSONObject(j);

                        List<String> list1 = new ArrayList<>();

                        list1.add(jsonObject2.getString("attribute_group_id"));
                        list1.add(jsonObject2.getString("name"));

                        JSONArray jsonArray5 = jsonObject2.getJSONArray("attribute");

                        for(int k=0;k<jsonArray5.length();k++){

                            JSONObject jsonObject3 = jsonArray5.getJSONObject(k);

                            List<String> list2 = new ArrayList<>();

                            list2.add(jsonObject3.getString("attribute_id"));
                            list2.add(jsonObject3.getString("name"));
                            list2.add(jsonObject3.getString("text"));
                        }
                    }
                }
            }
        }catch (IOException |JSONException e){
            Log.e("Error",e.getMessage());
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        dialog.dismiss();
      //  super.onPostExecute(aVoid);
       // new SetImageView(image).execute(thumbDiscount.get(j));
        textView.setText(s);
        new SetImage(imageView).execute(s1);
        for(int i=0;i<mainImage.size();i++){

            ImageView imageView = new ImageView(ProductInner.this);
            new SetImage(imageView).execute(mainImage.get(i));

            linearLayout.addView(imageView, i);
        }
    }
}

public class SetImage extends AsyncTask<String,Void,Bitmap>{

    ImageView bitmap;


    public SetImage(ImageView bitmap){
        this.bitmap = bitmap;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Bitmap doInBackground(String... params) {

        String urldisplay = params[0];
        Bitmap bitmap = null;
        try{
            InputStream in = new java.net.URL(urldisplay).openStream();
            bitmap = BitmapFactory.decodeStream(in);
        }catch (IOException e ) {
            e.getMessage();
            //    Log.e("Error :",e.getMessage());
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        bitmap.setImageBitmap(result);
        //super.onPostExecute(bitmap);
    }

}

}

My Json Array Link is here

我的 Logcat 显示此错误:-

E/Error: Value {"id":"80","name":"Bag_80","model":"
","reward":"0","points":"0","image":"http://opencart.codeniques.com/shopping/image/cache/data/product/bag_81_1-1000x1000.JPG","images":["http://opencart.codeniques.com/shopping/image/cache/data/product/bag_80_2-80x80.JPG"],"price":1175,"minimum":"1","newprice":1163.25,"qty":4,"rating":0,"description":""} at productdata of type org.json.JSONObject cannot be converted to JSONArray`

如日志中所示:

productdata of type org.json.JSONObject cannot be converted to JSONArray

表示 productdata 密钥用于 JSONObject 以响应服务器的 json 而不是 JSONArray

所以获取 productdata 键值作为 JSONObject 而不是 JSONArray :

JSONObject jsonObject1 = jsonObject.getJSONObject("productdata");

productdata 不是 JSONArray 而是 JSONObject,因此您必须更改

JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");

JSONObject jsonObject1 = jsonObject.getJSONObjct("productdata");

您获取的 productData 是一个 JSONArray,而它是一个包含图像 JsonArray 的 JsonObject,正如我在您提供的 json link 中看到的那样

JSONArray jsonArray1 = jsonObject.getJSONArray("productdata");

那行代码应该是一个 JsonObject