1

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;
            }
10
  • nothing is coming as value in log e Commented Aug 17, 2015 at 5:58
  • $services=mysql_query("select service_name from services"); $ser=''; while($fservices=mysql_fetch_array($services)) { $ser.=$fservices['service_name'].","; } $newser = explode($ser,","); print_r($newser); echo "<br/>".$ser."<br/>"; if(mysql_num_rows($services)) { $arr = array('result' => "sucess", 'data' => $ser, 'msg' => ' Sucessfull'); } else { $arr = array('result' => "error", 'msg' => 'Unsucessfull'); } echo json_encode($arr); ?> Commented Aug 17, 2015 at 5:59
  • is there any problem in php side Commented Aug 17, 2015 at 6:02
  • check SERVIVECS_URL directly in your browser with parameter and make sure your php code is correct. Commented Aug 17, 2015 at 6:03
  • First get the root object..then extract array Commented Aug 17, 2015 at 6:17

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

$services=mysql_query("select service_name from services"); $ser=''; while($fservices=mysql_fetch_array($services)) { $ser.=$fservices['service_name'].","; } $newser = explode($ser,","); print_r($newser); echo "<br/>".$ser."<br/>"; if(mysql_num_rows($services)) { $arr = array('result' => "sucess", 'data' => $ser, 'msg' => ' Sucessfull'); } else { $arr = array('result' => "error", 'msg' => 'Unsucessfull'); } echo json_encode($arr); ?>
<?php $services = mysql_query("select service_name from services"); $ser = ''; $arrJson = array(); while ($fservices = mysql_fetch_array($services)) { $data=$fservices['service_name']; $arrJson = array("serv_name", $data); } $arrJson1 = array("data", $arrJson); if (mysql_num_rows($services)) { $arr = array('result' => "sucess", 'data' => $arrJson1, 'msg' => ' Sucessfull'); } else { $arr = array('result' => "error", 'msg' => 'Unsucessfull'); } echo json_encode($arr); ?>
0

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();
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.