簡體   English   中英

如何在來自Android JSON Post Volley的PHP中解碼JSON

[英]How to decode json in php coming from android json post volley

Gooday對不起,我的英語不好,並且還是編程新手。 我正在嘗試解碼從android volley JSON POST拋出的JSONObject。 現在,我可以從Android接收到服務器的響應,並且可以通過TOAST進行確認。 我的問題是如何使用PHP從服務器端正確解碼JSON。 此JSON數據將用於在MySQL中插入新數據。 我只是不知道正確的方法,並且很難為此找到解碼設置的答案。 請您在這里提供任何建議。

public void testOrder (ArrayList<String> order_id,
                                      ArrayList<String> uname,
                                      ArrayList<String> prod_name,
                                      ArrayList<String> prod_id,
                                      ArrayList<String> quantity,
                                      ArrayList<String> branches,
                                      ArrayList<String> totalPrice,
                                      int itemIteration){
        JSONObject obj = null;
        JSONArray jsonArray = new JSONArray();
        final JSONObject finalobject = new JSONObject();
        for (int i = 0; i < 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<String>()
                {
                    @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<String, String> getParams()
            {
                Map<String, String> params = new HashMap<>();
                params.put("tag", finalobject.toString());
//                String obj = finalobject.toString();
//                Toast.makeText(PlaceOrder.this,obj,Toast.LENGTH_SHORT).show();
                return params;
            }
        };
        AppController.getmInstance().addToRequesQueue(strRequest);
    }
}

PHP代碼

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){

$response = array("ORDERLIST" => $_POST["tag"]);
//$response = array($_POST["tag"]);
//$my_array = json_decode($response,true);

echo json_encode(str_replace('\"','"',$response));

     echo "RECEIVED";

} else{

       echo "nothing";
}

我使用Log.d進行響應:

D /響應:

{"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\"}]}"}

使用echo $ content后,這將顯示在我的吐司上

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&

編輯的PHP代碼

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";
}

(已編輯)

嘗試這個:

$content   = file_get_contents( "php://input" );
$response  = json_decode( $content, TRUE );
$orderlist = json_decode( $response['ORDERLIST'], TRUE );

從PHP文檔:

php:// input是一個只讀流,允許您從請求正文中讀取原始數據。 對於POST請求,最好使用php:// input而不是$ HTTP_RAW_POST_DATA,因為它不依賴於特殊的php.ini指令。

解碼json數據后,您將在$orderlist獲得一個關聯數組。 現在,您可以選擇將所需的數組值保存到數據庫中。

因此(即): $orderlist[0][order_id]$orderlist[1][uname]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM