【问题标题】:How to convert android JSON data into PHP array如何将android JSON数据转换为PHP数组
【发布时间】:2012-09-15 01:03:19
【问题描述】:

我有一个格式为

的 JSON 数据 [{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount": "1"},{"用户名":"test"}]

现在我想在我的 PHP Web 系统中获取这些数据。我正在将这些数据从 android 应用程序发送到 PHP 服务器。我正在使用下面的代码将其发送到 Web 服务器。

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();
.....

在发送之前我打印

jArray.toString()
并得到输出
[{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount":"1"},{"username":"test"}]

我想知道如何从 PHP 系统中获取这些值。谁能帮帮我?

在通过 HTTPRequest 发送之前,params 变量值的输出如下所示

[cartdata=[{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount":"1"},{"username":"UWUDAMITH"}]]

【问题讨论】:

    标签: php android json httprequest httpresponse


    【解决方案1】:

    使用json_decode()

    http://php.net/manual/en/function.json-decode.php

    用法:json_decode($json, true) //将json解码为关联数组

    【讨论】:

      【解决方案2】:

      http://php.net/manual/en/function.json-decode.php 这应该会派上用场。 还要检查 http://php.net/manual/en/function.json-encode.php 是否与该功能完全相反。

      【讨论】:

        【解决方案3】:

        当您以 urlencoded 格式发送数据时,这些值将出现在 $_POST PHP 的数组中!从那里您可以使用 json_decode() 对它们进行解码:

        $json = $_POST['json']; // assumes JSON is posted as "json" POST variable
        $data = url_decode($json, true); // gets you associative arrays, not objects
        

        您唯一需要做的就是在 POST 中将 JSON 字符串作为“json”变量发送,并在请求正文中进行适当的编码(URL 编码)。

        【讨论】:

        • 我的工作方式是否正确。是httppost.setEntity(new UrlEncodedFormEntity(params));正确还是有其他方法可以这样做。任何示例代码都会有所帮助。
        • 似乎网络系统无法从android获取JSON。但是httprequest方法100%有效,因为我得到了响应。
        • @Damith:在服务器上检索时,没有限制限制您获取 Android 发送的 JSON 数据。唯一的事情是以 urlencoded 形式发送。在这部分我无法帮助您,但我可以指导您找到解决方案:stackoverflow.com/q/4330392/548696
        • 感谢您提供的信息。我仍在努力解决问题。
        【解决方案4】:

        这可能不是最好的方法,因为我是 Java/Android 新手,但它对我有用 :)

              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
              }
        

        这将使用 $_POST['id'] = 1 将帖子发送到服务器,然后将 $_POST['json'] = 发送到您的 json 数据;

        这里是save.php。我实际上将它保存到 MySQL,所以你。

            <?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);
            ?>
        

        【讨论】:

        • 我遵循了您的代码,但网络系统总是为空。发生这种情况的可能性有多大。在发送之前,我的 NameValuePair 对象值看起来像 [username=test, cartdata=[{"product_id":"33","amount":"1"},{"product_id":"34","amount":"3"},{"product_id":"10","amount":"1"}]]
        • @Damith 确保在使用 $_POST vals 之前添加一些额外的检查,不要进行任何 sql 注入。另外,我很抱歉发布 MySQL,应该使用 MySQLi。
        猜你喜欢
        • 2014-11-25
        • 1970-01-01
        • 2016-09-09
        • 2018-05-15
        • 2015-07-02
        • 2016-07-07
        • 2013-06-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多