title: How to decode json in php coming from android json post volley
body: <p>Gooday sorry for my bad english and a newbie in programming. I'm trying to decode a JSONObject thrown from an android volley JSON POST. Right now I'm able to received from the Android the response from the server and I can confirm that thru TOAST. My question is how to properly decode JSON from the server side using PHP. This JSON data will be use for inserting new data in MySQL. I just don't know the proper way and having hard time finding out the answer for the decoding setup for this. Please can you give any advice here.</p>
  
  <pre><code>public void testOrder (ArrayList&lt;String&gt; order_id,
                                        ArrayList&lt;String&gt; uname,
                                        ArrayList&lt;String&gt; prod_name,
                                        ArrayList&lt;String&gt; prod_id,
                                        ArrayList&lt;String&gt; quantity,
                                        ArrayList&lt;String&gt; branches,
                                        ArrayList&lt;String&gt; totalPrice,
                                        int itemIteration){
          JSONObject obj = null;
          JSONArray jsonArray = new JSONArray();
          final JSONObject finalobject = new JSONObject();
          for (int i = 0; i &lt; itemIteration; i++) {
              obj = new JSONObject();
              Log.d("OBJECT_COUNTER", String.valueOf(i));
              try {
                  obj.put("order_id",  order_id.get(i));
                  Log.d("ORDER_ID",    order_id.get(i));
                  obj.put("uname",     uname.get(i));
                  Log.d("USERNAME",    uname.get(i));
                  obj.put("prod_name", prod_name.get(i));
                  Log.d("PROD_NAME",   prod_name.get(i));
                  obj.put("quantity",  quantity.get(i));
                  Log.d("PROD_QUANT",  quantity.get(i));
                  obj.put("branch",    branches.get(i));
                  Log.d("PROD_BRANCHES", branches.get(i));
                  obj.put("totalPrice",  totalPrice.get(i));
                  Log.d("TOTAL_PRICE",   totalPrice.get(i));
              } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              jsonArray.put(obj);
          }
              try {
                  finalobject.put("ORDER_LIST", jsonArray);
                  test = jsonArray.toString();
  //                Toast.makeText(this,test,Toast.LENGTH_SHORT).show();
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
          StringRequest strRequest = new StringRequest(Request.Method.POST, Config.TRY_JSON_POST,
                  new Response.Listener&lt;String&gt;()
                  {
                      @Override
  
                      public void onResponse(String response)
                      {
                          Toast.makeText(PlaceOrder.this, response, Toast.LENGTH_SHORT).show();
  //                       
                      }
                  },
                  new Response.ErrorListener()
                  {
                      @Override
                      public void onErrorResponse(VolleyError error)
                      {
                          Toast.makeText(PlaceOrder.this, error.toString(), Toast.LENGTH_SHORT).show();
                      }
                  })
          {
              @Override
              protected Map&lt;String, String&gt; getParams()
              {
                  Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
                  params.put("tag", finalobject.toString());
  //                String obj = finalobject.toString();
  //                Toast.makeText(PlaceOrder.this,obj,Toast.LENGTH_SHORT).show();
                  return params;
              }
          };
          AppController.getmInstance().addToRequesQueue(strRequest);
      }
  }
  </code></pre>
  
  <p>PHP CODE</p>
  
  <pre><code>&lt;?php 
  if($_SERVER['REQUEST_METHOD']=='POST'){
  
  $response = array("ORDERLIST" =&gt; $_POST["tag"]);
  //$response = array($_POST["tag"]);
  //$my_array = json_decode($response,true);
  
  echo json_encode(str_replace('\"','"',$response));
  
       echo "RECEIVED";
  
  } else{
  
         echo "nothing";
  }
  </code></pre>
  
  <p>I use Log.d for the response:</p>
  
  <p>D/Response: </p>
  
  <pre><code>{"ORDERLIST":"{\"ORDER_LIST\":[{\"order_id\":\"1755\",\"uname\":\"jordan\",\"prod_name\":\"ABC\",\"quantity\":\"1\",\"branch\":\"Dapitan\",\"totalPrice\":\"23500\"},{\"order_id\":\"1755\",\"uname\":\"jordan\",\"prod_name\":\"AMD Kaveri A4-7300 APU HD8470D 2-Core 2GB 500GB CPU Package\",\"quantity\":\"1\",\"branch\":\"Dapitan\",\"totalPrice\":\"23500\"}]}"}
  </code></pre>
  
  <p>After using echo $content this is displayed on my toast</p>
  
  <pre><code>D/Response: DATA:tag=%7B%22ORDER_LIST%22%3A%5B%7B%22order_id%22%3A%221755%22%2C%22uname%22%3A%22jordan%22%2C%22prod_name%22%3A%22ABC%22%2C%22quantity%22%3A%221%22%2C%22branch%22%3A%22Dapitan%22%2C%22totalPrice%22%3A%2223500%22%7D%2C%7B%22order_id%22%3A%221755%22%2C%22uname%22%3A%22jordan%22%2C%22prod_name%22%3A%22AMD+Kaveri+A4-7300+APU+HD8470D+2Core+2GB+500GB+CPU+Package%22%2C%22quantity%22%3A%221%22%2C%22branch%22%3A%22Dapitan%22%2C%22totalPrice%22%3A%2223500%22%7D%5D%7D&amp;
  </code></pre>
  
  <p>edited PHP code</p>
  
  <pre><code>if($_SERVER['REQUEST_METHOD']=='POST'){
  
  $content   = file_get_contents( "php://input" );
  $response  = json_decode($content,TRUE);
  $orderlist = json_decode($response['ORDER_LIST'], TRUE);
  
  //echo $orderlist[0][order_id][0];
  
  echo "DATA: ";
  
  foreach($orderlist AS $row ){
    echo $row[uname];
  } 
  
        echo $response;
  } else{
        echo "nothing";
  }
  </code></pre>