1

I want to know how to parse JSON array inside into another JSON array.I am new in android developing please help me and Thanks in advance.my web service url here my code:-

public class  ProductsAsynTask extends AsyncTask<String,Void,Boolean>{

    ProgressDialog dialog;

    protected void onPreExecute(){
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, Please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    protected Boolean doInBackground(String... param){
        try {

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(param[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 jArray = jsonObject.getJSONArray("product");

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

                    JSONObject jsonObject1 = jArray.getJSONObject(i);

                    Products products = new Products();

                    products.setId(jsonObject1.getString("id"));
                    products.setName(jsonObject1.getString("name"));
                    products.setManufacturer(jsonObject1.getString("manufacturer"));
                    products.setModel(jsonObject1.getString("model"));
                    products.setReward(jsonObject1.getString("reward"));
                    products.setPoints(jsonObject1.getString("points"));
                    products.setImage(jsonObject1.getString("image"));
                    products.setPrice(jsonObject1.getString("price"));
                    products.setSpecial(jsonObject1.getString("special"));

                    JSONArray jArray1 = jArray.getJSONArray(i);

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

                        JSONObject jsonObject2 = jArray1.getJSONObject(j);

                        products.setDiscounts_quantity(jsonObject2.getString("quantity"));
                        products.setDiscounts_price(jsonObject2.getString("price"));
                    }
                    JSONArray jArray2 = jArray.getJSONArray(i);

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

                        JSONObject jsonObject2 = jArray2.getJSONObject(j);

                        products.setOptions_product_option_id(jsonObject2.getString("product_option_id"));
                        products.setOptions_option_id(jsonObject2.getString("option_id"));
                        products.setOptions_name(jsonObject2.getString("name"));
                        products.setOptions_type(jsonObject2.getString("type"));

                        JSONArray jsonArray3 = jArray2.getJSONArray(j);

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

                            JSONObject jsonObject3 = jsonArray3.getJSONObject(k);

                            products.setOptions_option_value_product_option_value_id(jsonObject3.getString("product_option_value_id"));
                            products.setOptions_option_value_option_value_id(jsonObject3.getString("option_value_id"));
                            products.setOptions_option_value_name(jsonObject3.getString("name"));
                            products.setOptions_option_value_image(jsonObject3.getString("image"));
                            products.setOptions_option_value_price(jsonObject3.getString("price"));
                            products.setOptions_option_value_price_prefix(jsonObject3.getString("price_prefix"));
                        }

                            products.setOptions_required(jsonObject2.getString("required"));
                    }

                            products.setMinimum(jsonObject1.getString("minimum"));
                            products.setRating(jsonObject1.getString("rating"));
                            products.setDescription(jsonObject1.getString("description"));

                            JSONArray jsonArray = jArray.getJSONArray(i);

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

                                JSONObject jsonObject2 = jsonArray.getJSONObject(j);

                                products.setAttribute_groups_attribute_group_id(jsonObject2.getString("attribute_group_id"));
                                products.setAttribute_groups_name(jsonObject2.getString("name"));

                                JSONArray jsonArray1 = jsonArray.getJSONArray(j);

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

                                    JSONObject jsonObject3 = jsonArray1.getJSONObject(k);

                                    products.setAttribute_groups_attribute_attribute_id(jsonObject3.getString("attribute_id"));
                                    products.setAttribute_groups_attribute_name(jsonObject.getString("name"));
                                    products.setAttribute_groups_attribute_text(jsonObject3.getString("text"));
                                }
                            }
                    productsList.add(products);
                }
                return true;
            }
            else if(status == 500){
                Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
            }

        }catch (ClientProtocolException e){
            Log.e("Error :",e.getMessage());
        }catch (IOException e){
            Log.e("Error :",e.getMessage());
        }catch (JSONException e){
            Log.e("Error :",e.getMessage());
        }catch (Exception e){
            Log.e("Error :",e.getMessage());
        }
        return false;
    }

    protected void onPostExecute(Boolean result){

        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(result == false){
            Toast.makeText(MainActivity.this,"Data is not Parsed",Toast.LENGTH_LONG).show();
        }
        else{
            ProductsAdapter adapter = new ProductsAdapter(getApplicationContext(),R.layout.content_main,productsList);
            listView.setAdapter(adapter);
        }
    }
}

I am new in android developing please help me and Thanks in advance.

2

4 Answers 4

1
@Override
        protected void onPostExecute(String res) {
            try {
                JSONArray jArray = new JSONArray(res.toString());
                if (jArray.length() == 0) {
                    txtNoDataFound1.setVisibility(View.VISIBLE);
                    txtNoDataFound1.setText("No Data Found !!");
                    txtNoDataFound1.setGravity(Gravity.CENTER
                            | Gravity.CENTER_HORIZONTAL);
                    expListView.setVisibility(View.GONE);
                } else {
                    ListHead.clear();
                    expListView.setVisibility(View.VISIBLE);

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

                        JSONObject json = jArray.getJSONObject(i);
                        getheadingid = json.getString("Headid");

                        listhead = new ListHeaderEntity(json.getString("Headid"),
                                json.getString("HeadServiceTitle"));

                        try {
                            JSONArray jArray1 = json.getJSONArray("subServiceData");

                            if (jArray1.length() == 0) {
                            } else {
                                listhead.setListitems(new ArrayList<CityEntity>());
                                for (int j = 0; j < jArray1.length(); j++) {
                                    JSONObject json1 = jArray1.getJSONObject(j);

                                    CityEntity listitem = new CityEntity();
                                    listitem.setLblcityid(json1.getString("subid"));
                                    listitem.setLblcityname(json1.getString("servicedetailTitle"));
                                    listhead.getListitems().add(listitem);
                                }
                            }
                        } catch (Exception objEx) {
                            objEx.printStackTrace();
                        }

                        ListHead.add(listhead);
                    }
                    listAdapter = new ServiceExpandAdapter(
                            getActivity(), ListHead, expListView);
                    expListView.setAdapter(listAdapter);

                    res1 = null;
                    selectedService.clear();
                    PutUtility objClient1 = new PutUtility();

                    try {
                        res1 = objClient1.getData("url");
                    } catch (Exception objEx) {
                        objEx.printStackTrace();
                    }

                    JSONArray jArray1 = new JSONArray(res1.toString());
                    if (jArray1.length() == 0) {
                    } else {
                        for (int i = 0; i < jArray1.length(); i++) {
                            JSONObject json = jArray1.getJSONObject(i);
                            //selectedvalue.add(Integer.parseInt(json.getString("subservice_id")));
                            int in=Integer.parseInt(json.getString("subservice_id"));
                                    selectedService.add(in);
                        }
                    }

                }
                mProgressDialog.dismiss();
            } catch (Exception objEx) {
                objEx.printStackTrace();
            }
        }
0
0

To take another array in an object you have to change

JSONArray jArray1 = jArray.getJSONArray(i);

to

JSONArray jArray1 = jsonObject1.getJSONArray("discounts");
0

In your json response "product" is not json array it is json object. Change like this in your doInBackground().

    protected Boolean doInBackground(String... param){
    try {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(param[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);


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

                Products products = new Products();

                products.setId(jsonObject1.getString("id"));
                products.setName(jsonObject1.getString("name"));
                products.setManufacturer(jsonObject1.getString("manufacturer"));
                products.setModel(jsonObject1.getString("model"));
                products.setReward(jsonObject1.getString("reward"));
                products.setPoints(jsonObject1.getString("points"));
                products.setImage(jsonObject1.getString("image"));
                products.setPrice(jsonObject1.getString("price"));
                products.setSpecial(jsonObject1.getString("special"));



                JSONArray jArray1 = jsonObject1.getJSONArray("discounts");

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

                    JSONObject jsonObject2 = jArray1.getJSONObject(j);

                    products.setDiscounts_quantity(jsonObject2.getString("quantity"));
                    products.setDiscounts_price(jsonObject2.getString("price"));
                }
                JSONArray jArray2 = jsonObject1.getJSONArray("options");

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

                    JSONObject jsonObject2 = jArray2.getJSONObject(j);

                    products.setOptions_product_option_id(jsonObject2.getString("product_option_value_id"));
                    products.setOptions_option_id(jsonObject2.getString("option_id"));
                    products.setOptions_name(jsonObject2.getString("name"));
                    products.setOptions_type(jsonObject2.getString("type"));

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

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

                        JSONObject jsonObject3 = jsonArray3.getJSONObject(k);

                        products.setOptions_option_value_product_option_value_id(jsonObject3.getString("product_option_value_id"));
                        products.setOptions_option_value_option_value_id(jsonObject3.getString("option_value_id"));
                        products.setOptions_option_value_name(jsonObject3.getString("name"));
                        products.setOptions_option_value_image(jsonObject3.getString("image"));
                        products.setOptions_option_value_price(jsonObject3.getString("price"));
                        products.setOptions_option_value_price_prefix(jsonObject3.getString("price_prefix"));
                    }

                        products.setOptions_required(jsonObject2.getString("required"));
                }

                        products.setMinimum(jsonObject1.getString("minimum"));
                        products.setRating(jsonObject1.getString("rating"));
                        products.setDescription(jsonObject1.getString("description"));

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

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

                            JSONObject jsonObject2 = jsonArray.getJSONObject(j);

                            products.setAttribute_groups_attribute_group_id(jsonObject2.getString("attribute_group_id"));
                            products.setAttribute_groups_name(jsonObject2.getString("name"));

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

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

                                JSONObject jsonObject3 = jsonArray1.getJSONObject(k);

                                products.setAttribute_groups_attribute_attribute_id(jsonObject3.getString("attribute_id"));
                                products.setAttribute_groups_attribute_name(jsonObject.getString("name"));
                                products.setAttribute_groups_attribute_text(jsonObject3.getString("text"));
                            }
                        }
                productsList.add(products);
            }
            return true;
        }
        else if(status == 500){
            Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
        }

    }catch (ClientProtocolException e){
        Log.e("Error :",e.getMessage());
    }catch (IOException e){
        Log.e("Error :",e.getMessage());
    }catch (JSONException e){
        Log.e("Error :",e.getMessage());
    }catch (Exception e){
        Log.e("Error :",e.getMessage());
    }
    return false;
}
4
  • JSONObject jsonObject1 = jArray.getJSONObject("product"); This jArray??
    – Anand Jain
    Commented Sep 21, 2015 at 10:57
  • That's my mistake changed jArray to jsonObject. Edited answer.
    – Ved
    Commented Sep 21, 2015 at 11:21
  • When i am trying your code its give a error JSONObject cannot be converted to JSONArray.
    – Anand Jain
    Commented Sep 21, 2015 at 12:24
  • Let us continue this discussion in chat.
    – Anand Jain
    Commented Sep 22, 2015 at 3:42
-1

Json will just parse the json file in to one json object and then you can just reference it.

1
  • i don't understand what are you trying to say please explain with example @Cameron White
    – Anand Jain
    Commented Sep 19, 2015 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.