Udit Roy
Udit Roy

Reputation: 85

android json object some issue in code not able to get the json array from php

I cant get the following array object from the php side

{"result":"sucess","data":["Painting service","Plumbing service", 
"Electrical service","Carpentry Services","Aluminium works",
"House Cleaning","Home Appliance","Glazing Cleaning",
"Yard Maintenance","Water Tank Cleaning",
"Electronics Services","Upholstery Services","DRY CLEANERS",""],"msg":" Sucessfull"}

this is my json responce from php side

and im using

   protected String doInBackground(String... args) {
                List<NameValuePair> userpramas = new ArrayList<NameValuePair>();
                //String a =(spinerplan.getSelectedItem().toString());
                userpramas.add(new BasicNameValuePair("package_type",glbstr_plan));

                JSONObject json = jsonParser.makeHttpRequest(CommonClass.SERVIVECS_URL, "POST",
                        userpramas);
                //Log.e("testing", "json url value=" + json);
                try {

                    String responce = json.getString("data");

                    JSONObject servicejson = new JSONObject(responce);
                    JSONArray jArray = servicejson.getJSONArray("data");

                    System.out.println("*****JARRAY*****" + jArray.length());

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

                        Log.e("testing", "responce" + json_data);
                    }




                    Log.e("testing", "responce" + responce);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return responce;
            }

Upvotes: 1

Views: 122

Answers (2)

Iamat8
Iamat8

Reputation: 3906

your data is JSONArray and you are passing it as JSONObject ...

public void parseJson(String json) {
    try {
            JSONObject obj = new JSONObject(json);
            JSONArray root = obj.getJSONArray("data");
            for (int i = 0; i < root.length(); i++) {
                JSONObject att = (JSONObject) root.getJSONObject(i);
                    // - your code- //
            }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Bhavesh
Bhavesh

Reputation: 331

I think your JSON response is wrong..

You need to generate response like this..

{"result":"sucess","data":[{"serv_name":"Painting service"},
{"serv_name":"Plumbing service"},{"serv_name":"Electrical service"},
{"serv_name":"Carpentry Services"},{"serv_name":"Aluminium works"},
{"serv_name":"House Cleaning"},{"serv_name":"Home Appliance"},
{"serv_name":"Glazing Cleaning"},{"serv_name":"Yard Maintenance"},
{"serv_name":"Water Tank Cleaning"},{"serv_name":"Electronics Services"},
{"serv_name":"Upholstery Services"},{"serv_name":"DRY CLEANERS"},
{"serv_name":""}],"msg":" Sucessfull"}

then you can parse the response using below code..

String responce = json.getString("data");
JSONObject servicejson = new JSONObject(responce);
JSONArray jArray = servicejson.getJSONArray("data");
System.out.println("*****JARRAY*****" + jArray.length());
for(int i=0; i<jArray.length(); i++){
   JSONObject json_data = jArray.getJSONObject(i);
   String serviceName = json_data.getString("serv_name");
   Log.e("testing", "responce" + serviceName);
}
Log.e("testing", "responce" + responce);

Upvotes: 1

Related Questions