How to convert android JSON data into PHP array
I have an JSON data in format of
[{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount":"1"},{"username":"test"}]
now i want to fetch those data in my PHP web system. I am sending those data into PHP server from an android application.I am using below code to send it to web server.
public JSONObject sendAndGetJSONfromURL(String url,List<NameValuePair> params){ InputStream is = null; String result = ""; JSONObject jArray = null; //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); httppost.setHeader("Content-type", "application/json"); httppost.setHeader("Accept", "application/json"); httppost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); .....
Before send it i print the
jArray.toString()and got the output as
[{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount":"1"},{"username":"test"}]
I want to know that how can i fetch those values from PHP system.Can anyone please help me?
Output of the params variable value looks like below before sending via HTTPRequest
[cartdata=[{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount":"1"},{"username":"UWUDAMITH"}]]
This might not be the best way to do it, as I am new to Java/Android, but it works for me :)
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yourdomain.com/save.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "1"));
nameValuePairs.add(new BasicNameValuePair("json",jArray.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
This will send a post to the server with a $_POST['id'] = 1 and then $_POST['json'] = to your json data;
Here is save.php. I actually save it to MySQL, so ya.
<?php
$server = "localhost";
$username = "user";
$password = "pass";
$database = "db";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);
$id = $_POST["id"];
$json = $_POST["json"];
$sql = "INSERT INTO comments (id, json) ";
$sql .= "VALUES ($id, '$json')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
} else {
echo "Comment added";
}
mysql_close($con);
echo json_decode($json);
?>
Use json_decode()
http://php.net/manual/en/function.json-decode.php
Usage: json_decode($json, true)
//decodes json to associative arrays
http://php.net/manual/en/function.json-decode.php This should come in handy. Also check http://php.net/manual/en/function.json-encode.php for the exact opposite of the function.
链接地址: http://www.djcxy.com/p/21942.html上一篇: 发送“POST”JSON数据到服务器