json_decode返回nempty值php

json_decode返回nempty值php,php,android,json,Php,Android,Json,我正在从我的android应用程序中获取json字符串,其中包含用于用户注册的数据。 在服务器端,我使用php将其插入数据库 下面是我将获得的json字符串 {"user_name":"Steve Jobs","user_email":"steave@apple.com","user_password":"nopassword","user_phone_number":"1234567890","club_member_id":"24"} 但当我将其转换为数组时,我将得到null值 <?

我正在从我的android应用程序中获取json字符串,其中包含用于用户注册的数据。 在服务器端,我使用php将其插入数据库

下面是我将获得的json字符串

{"user_name":"Steve Jobs","user_email":"steave@apple.com","user_password":"nopassword","user_phone_number":"1234567890","club_member_id":"24"}
但当我将其转换为数组时,我将得到null

<?php

  //$_POST contains the json string. 
  $data = json_decode($_POST,true);


?>

您的json数据可能会出现在POST的某个数组索引中,而不是在完整的全局数组中。您的代码应该是这样的

$data = json_decode($_POST['some_json_field'], true);

看到您进行编辑后,似乎您毕竟是将json作为键值对的一部分发送的,只是使用了一个奇怪的键名(POST),这让人感到困惑

您应该能够通过以下方式获取数据:

$data = json_decode($_POST['POST'],true);
为清楚起见,将密钥名称更改为更合理的名称可能是有意义的:

nameValuePairs.add(new BasicNameValuePair("json", jsonParams.toString()));
然后您可以通过以下方式访问:

$data = json_decode($_POST['json'],true);
或者,也可以将数据作为json发送,而不是键值对:

try {
        jsonParams.put("user_name", "Steve Jobs");
        jsonParams.put("user_email", "steave@apple.com");
        jsonParams.put("user_password", "nopassword");
        jsonParams.put("user_phone_number", "1234567890");
        jsonParams.put("club_member_id", "24");
        StringEntity params = new StringEntity(jsonParams.toString());
        httppost.addHeader("content-type", "application/json");
        httppost.setEntity(params);
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    }
然后使用我的原始答案中描述的php代码

原始答复:

$\u POST
是一个数组,当接收到包含键值对的POST请求时自动填充(
contentType application/x-www-form-urlencoded

如果发布json数据(
contentType应用程序/json
),则不会填充
$\u post
,而是需要解析输入流:

$data = json_decode(file_get_contents('php://input'), true);
试试这个:

$data = json_decode($_POST['string_name_in_which_json_string_is_present'],true);

您不能使用$\u POST获取值。请尝试:

$data = json_decode(file_get_contents('php://input'), true);
这应该可以做到


如果请求正文不是标准的urlencoded格式,则不会填充$\u POST。

能否执行
var\u转储($\u POST)?在这里发布结果。您将JSON字符串放入$\u Post中的哪个变量?如何将其转换为数组?你能给我看看代码吗?这将得到一个空数组
$data = json_decode(file_get_contents('php://input'), true);