title: How to return JSON Object from PHP to read from Android App
body: <p>I have the following <code>MySQL</code> table.</p>
  
  <p><img src="https://i.stack.imgur.com/4oOJy.png" alt="enter image description here"></p>
  
  <p>I need to send the dealer name from <code>android</code> app and send all matching rows to the app.I dont know how to set the <code>php</code> output.I know i should encode using <code>json_encode(array('stuff'));</code> </p>
  
  <p>But i dont know how to format the data.Been checking <a href="http://www.w3schools.com/json/" rel="nofollow noreferrer">http://www.w3schools.com/json/</a> It shows an example </p>
  
  <pre><code>{"employees":[
      {"firstName":"John", "lastName":"Doe"}, 
      {"firstName":"Anna", "lastName":"Smith"},
      {"firstName":"Peter", "lastName":"Jones"}
  ]}
  </code></pre>
  
  <p>Should i use the same format?How to read this from android ?Im referring <a href="http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php" rel="nofollow noreferrer">http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php</a></p>
  
  <p>EDIT:</p>
  
  <pre><code>    &lt;?php
  
    //$con goes here 
      $result=$conn-&gt;query("SELECT pname FROM products");
      $response["stuff"] = array();
  
          while($row = $result-&gt;fetch_assoc())    {
  
              $stuff= array();
  
              /* ADD THE TABLE COLUMNS TO THE JSON OBJECT CONTENTS */
              $stuff["pname"] = $row['pname'];
              array_push($response["stuff"], $stuff);
  
              // $response[] = $row;
          }
          // success
          $response["success"] = 1;
          echo(json_encode($response));
  
  
      /* CLOSE THE CONNECTION */
      mysqli_close($conn );
      ?&gt;
  </code></pre>
  
  <p>JAVA Code:</p>
  
  <pre><code>package com.example.testing;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.NameValuePair;
  import org.json.JSONArray;
  import org.json.JSONObject;
  
  import android.os.AsyncTask;
  import android.util.Log;
  
  class testing extends AsyncTask&lt;Void, Void, Void&gt;   {
      JSONParser jParser; 
      String URL_TO_PHP = "http://coolsite.com/dashboard/test.php";
      String TAG_SUCCESS = "success";
      String TAG_STUFF = "stuff";
  
      protected Void doInBackground(Void... args) {
  
          /* Building Parameters */
          List &lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  
          /* getting JSON string from URL */
          JSONObject json = jParser.makeHttpRequest(URL_TO_PHP, "GET", params);
  
          try {
              /* Checking for SUCCESS TAG */
              int success = json.getInt(TAG_SUCCESS);
              if (success == 1) {
                  JSONArray JAStuff = json.getJSONArray(TAG_STUFF);
  
                  /** CHECK THE NUMBER OF RECORDS **/
                  int intStuff = JAStuff.length();
  
                  if (intStuff != 0)  {
  
                      for (int i = 0; i &lt; JAStuff.length(); i++) {
                          JSONObject JOStuff = JAStuff.getJSONObject(i);
                          Log.e("ALL THE STUFF", JOStuff.toString());
  
                      }
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return null;
      }
  } 
  </code></pre>
  
  <p><strong><em>Main activity</em></strong> </p>
  
  <pre><code>package com.example.testing;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
  
      }
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.button1:
            new testing().execute();
              break;
          default:
              break;
          }
  
      }
  
  
  }
  </code></pre>
  
  <p><strong><em>Manifest</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.testing"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="19"
          android:targetSdkVersion="21" /&gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong><em>JSON Response in PHP</em></strong></p>
  
  <pre><code>{"stuff":[{"pname":"Advocates"},{"pname":"Notaries"},{"pname":"Agriculture \/ Fertilizer"},{"pname":"Airconditioners"},{"pname":"Animation"},{"pname":"Airline Companies"},{"pname":"Aluminium Fabricators"},{"pname":"Amusement Parks"},{"pname":"Pets shop"},{"pname":"Association \/ Societies"},{"pname":"Astrology"},{"pname":"Attestation Service"},{"pname":"Auditorium"},{"pname":"Automobiles Accessories"},{"pname":"Automobile Dealers"},{"pname":"Ayurveda"},{"pname":"Gifts Articles"},{"pname":"Bags"},{"pname":"Batteries"},{"pname":"Bearings"},{"pname":"Beauty Parlours \/ Spa"},{"pname":"Magazine Dealers"},{"pname":"Building Contractors"},{"pname":"Building Materials"},{"pname":"Cameras"},{"pname":"Cast Iron Products"},{"pname":"Home Delivery"},{"pname":"Cements Wholesale \/ Retail"},{"pname":"Chemicals"},{"pname":"Contractors"},{"pname":"Consultants"},{"pname":"Parcel Services"},{"pname":"Crockery"},{"pname":"Dental Clinics"},{"pname":"Cyber Parks"},{"pname":"Dance School\/Costumes"},{"pname":"Diagnostics Labs&amp; X-Rays"},{"pname":"Distance Education"},{"pname":"Driving Schools"},{"pname":"Dry Cleaners"},{"pname":"DTH Services"},{"pname":"DTP Centers"},{"pname":"Electrical Contractors \/ Services"},{"pname":"Electronics Equipment \/ Servicing"},{"pname":"Coaching Centre"},{"pname":"Event Management"},{"pname":"Fancy \/ Stationery"},{"pname":"Pooja stores"},{"pname":"Food Products"},{"pname":"Foot Wear"},{"pname":"Foreign Exchange"},{"pname":"Furniture shop"},{"pname":"Granites \/ Marbles"},{"pname":"Hospitals \/ Clinics"},{"pname":"Computer Accessories"},{"pname":"Kitchen Cabinets"},{"pname":"Library"},{"pname":"Lights \/ Fittings"},{"pname":"Mobile Phone Accesseries"},{"pname":"Machinery \/ Tools"},{"pname":"Timber Manufacturer"},{"pname":"Real Estate Agents"},{"pname":"Security Services"},{"pname":"Solar Products"},{"pname":"Health &amp; Fitness"},{"pname":"Travel Agencies"},{"pname":"Used Car \/ Bikes"},{"pname":"Waste Management"},{"pname":"Watches &amp; Clocks"},{"pname":"Handicrafts"},{"pname":"Doors &amp; Windows"}],"success":1}
  </code></pre>