将JSON数组的值放入PHP变量

时间:2015-08-25 11:49:37

标签: php android mysql json

我正在尝试从JSON获取数组项以保存在mysql数据库中。从android收到时,它的格式为:

array(1) {
["records"]=>
string(215) "[{"total":"12.0","product":"Sample1","id":"0","qty":"2","invoice":"2.5082015071735E13"},{"total":"15870.0","product":"Sample2","id":"0","qty":"23","invoice":"2.5082015071735E13"}]"

PHP代码:

if($_POST)
{
echo "Smething was sent";

$JSON_Entry = $_POST["Entry"];


$obj = json_decode($JSON_Entry,true);



foreach ($obj as $key => $value) {
    foreach($value as $index => $item)
    {
        echo $index;
    }
}

}
}

我只是做了一个foreach声明:

foreach ($obj as $key => $value) {
    echo $value;
}

显示:

[{"total":"12.0","product":"Uganda Waragi 200ml 24x01","id":"0","qty":"2","invoice":"2.5082015071735E13"},{"total":"15870.0","product":"Gilbeys Gin 35cl 24x01 EL","id":"0","qty":"23","invoice":"2.5082015071735E13"}]

尝试实现嵌套的foreach显示提供的无效参数错误。有没有办法可以有效地获取不同的值来保存变量,还是我的语法有问题?

UPDATE :: print_r($ obj)输出:

Array
(
   [records] => [ {"total":"12.0",
                   "product":"Uganda Waragi 200ml 24x01",
                   "id":"0",
                   "qty":"2",
                   "invoice":"2.5082015071735E13"}, 

                  {"total":"15870.0",
                   "product":"Gilbeys Gin 35cl 24x01 EL",
                   "id":"0",
                   "qty":"23",
                   "invoice":"2.5082015071735E13"}
                 ]
)

更新2

echo $_POST["Entry"];  outputs : 

{"records":"[{\"total\":\"12.0\",\"product\":\"Sample1\",\"id\":\"0\",\"qty\":\‌​"2\",\"invoice\":\"2.5082015071735E13\"},{\"total\":\"15870.0\",\"product\":\"Sam‌​ple2\",\"id\":\"0\",\"qty\":\"23\",\"invoice\":\"2.5082015071735E13\"}]"}

更新3: JAVA代码:

ArrayList<RecieptHeader> invoiceList = db.getInvoiceHeader();
        ArrayList<SalesReciepts> entryList = db.getSalesRecords();

        ArrayList<String> contact = new ArrayList<String>();


        List<NameValuePair> postVars = new ArrayList<NameValuePair>();


        JSONObject JSONheader = new JSONObject();

        JSONArray recordsJsonArray = new JSONArray();

        for (int i = 0; i < entryList.size(); i++) {

            try {
                JSONObject JSONentry = new JSONObject(); // here you create a new JSONObject

                JSONentry.put("id", String.valueOf(entryList.get(i).getEntryId()));
                JSONentry.put("invoice",String.valueOf(entryList.get(i).getInvoice_id()));
                JSONentry.put("product", String.valueOf(entryList.get(i).getProduct()));
                JSONentry.put("qty", String.valueOf(entryList.get(i).getQty()));
                JSONentry.put("total", String.valueOf(entryList.get(i).getTotal()));

                recordsJsonArray.put(JSONentry); // here you add the item to your array
            }
            catch(JSONException e) {
                e.printStackTrace();
            }

        }

        JSONObject sent = new JSONObject();

        try {

                sent.put("records", String.valueOf(recordsJsonArray));
        }
        catch(JSONException e) {
            e.printStackTrace();
        }





        try {
            JSONheader.put("Invoice", String.valueOf(invoiceList));
        }
        catch(JSONException e) {
            e.printStackTrace();
        }






        postVars.add(new BasicNameValuePair("Entry", String.valueOf(sent)));
        postVars.add(new BasicNameValuePair("Header", String.valueOf(JSONheader)));

        //Declare and Initialize Http Clients and Http Postsz
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(POST_PRODUCTS);

        //Format it to be sent
        try {
            httppost.setEntity(new UrlEncodedFormEntity(postVars));

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

        /* Send request and Get the Response Back */
        try {

            HttpResponse response = httpclient.execute(httppost);
            String responseBody = EntityUtils.toString(response.getEntity());


            Log.e("response:", responseBody );

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

            Log.v("MAD", "Error sending... ");


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

            Log.v("MAD", "Error sending... ");


        }



        return result;

2 个答案:

答案 0 :(得分:0)

使用json_decode:

foreach ($obj as $key => $value) {
    var_dump(json_decode($value));
}

我认为json字符串应该以{}括号开头,而不是[]

答案 1 :(得分:0)

您可以使用PHP的 json_decode()函数,这会将JSON字符串转换为数组。