如何提取JSON数据,并在MySQL中插入PHP [英] How to extract json data and insert in mysql php

查看:161
本文介绍了如何提取JSON数据,并在MySQL中插入PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其实我是新来的Andr​​oid Web服务,请帮助我
我的问题,我从手机客户端发送的JSON恩codeD数据,我在服务器端获取的JSON数据,以便
客户端code:

Actually I am new to android web services so please help me my problem I am sending json encoded data from mobile client and I am getting json data on server side so that client side code:

  mJobject.put("userName", contactname.getText().toString());
                     mJobject.put("phonenumber",phonenumber.getText().toString() );
                     mJArray.put(mJobject);
                     Log.v(Tag, "^============send request" + mJArray.toString());
                    contactparams.add(new BasicNameValuePair("contactdetails", mJArray.toString()));
                     Log.v(Tag, "^============send request params" + mJArray.toString());
                    jsonString=WebAPIRequest.postJsonData("http://localhost/contactupload/contactindex.php",contactparams);
public static String postJsonData(String url, List<NameValuePair> params) {
    String response_string = new String();

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
 //   httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
    try {
            httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));


          /*  String paramString = URLEncodedUtils.format(params, HTTP.UTF_8);
            String sampleurl = url + "" + paramString;
            Log.e("Request_Url", "" + sampleurl);*/

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            if (response != null) {
                    InputStream in = response.getEntity().getContent();
                    response_string = WebAPIRequest.convertStreamToString(in);

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

    return response_string;

和PHP端我做

<?php
$json_data=$_POST['contactdetails'];
$data=json_decode($json_data);
print_r($data);
?>

我得到响应

 Array
 (
   [0] => stdClass Object
    (
           [phone number] => 5555
           [username] => xfg
      )
 )

让我怎么能提取PHP JSON数据并在mysql中插入

so how can I extract json data in php and insert in mysql

推荐答案

不要服用点这样的..

Do somehting like this..

<?php
$json_data=$_POST['contactdetails'];
$data=json_decode($json_data, true); // Added true flag

// You can access your variables like this..
echo $data['phone number'];// prints "5555"
echo $data['username']; // prints "xfg"

//do your db connection...

// execute your query with those variables...

?>

这篇关于如何提取JSON数据,并在MySQL中插入PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆