2016-01-07 101 views
0

我從我的android應用程序中獲取包含用戶註冊數據的json字符串。 在服務器端,我使用PHP將其插入數據庫。json_decode返回nempty值php

這裏是JSON字符串,我會得到

{"user_name":"Steve Jobs","user_email":"[email protected]","user_password":"nopassword","user_phone_number":"1234567890","club_member_id":"24"} 

但是,當我把它轉換到一個數組我會得到值。

<?php 

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


?> 

$data會得到空字符串

我將如何解決這個問題。

UPDATE

內容file_get_contents('php://input')

POST =%7B%22user_name%22%3A%22Steve +作業%22%2C%22user_email%22%3A%22steave%40apple.com的%22%2C%22user_password%22%3A%22nopassword%22%2C%22user_phone_number%22%3A%221234567890%22%2C%22club_member_id%22%3A%2224%22%7

JAVA

private void onSignup() throws IOException { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(url); 

//   Add your data 

     try { 
      jsonParams.put("user_name", "Steve Jobs"); 
      jsonParams.put("user_email", "[email protected]"); 
      jsonParams.put("user_password", "nopassword"); 
      jsonParams.put("user_phone_number", "1234567890"); 
      jsonParams.put("club_member_id", "24"); 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("POST", jsonParams.toString())); 
      Log.e("mainToPost", "mainToPost" + nameValuePairs.toString()); 
      // Use UrlEncodedFormEntity to send in proper format which we need 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 


     } catch (JSONException e) { 

     } 

     JSONParser jParser = new JSONParser(); 

     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     Log.e("Data", String.valueOf(json)); 
     try { 

      // Getting JSON Array 
      JSONArray user = json.getJSONArray("POST"); 
      Log.e("JSON ARRAY", String.valueOf(user)); 
      ArrayList<String> stringArray = new ArrayList<String>(); 
      for (int i = 0, count = user.length(); i < count; i++) { 
       try { 
        JSONObject jsonObject = user.getJSONObject(i); 
        String name = jsonObject.getString(TAG_NAME); 
        stringArray.add(name); 
        Log.e("JSON ARRAY", String.valueOf(stringArray)); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
//    name1.setText(String.valueOf(stringArray)); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } 
+2

你可以做'var_dump($ _ POST);'?在此發佈結果。 – Daan

+0

你將JSON字符串放入$ _POST中的哪個變量? –

+0

你如何將它轉換爲數組?你可以顯示該代碼 –

回答

0

您的JSON數據可能在POST的一些數組索引正在添加不完整的全球陣列你的代碼應該是這樣的

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

在看到您編輯,看來你要發送的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", "[email protected]"); 
     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代碼在我原來的答覆

原來的答案:

$_POST是一個數組,當接收到POST請求時,包含密鑰值對(contentType application/x-www-form-urlencoded)自動填充。

如果您張貼JSON數據(contentType application/json),$_POST將不被填充,而不是你需要解析輸入流:

$data = json_decode(file_get_contents('php://input'), true); 
0

試試這個:

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

這將得到一個空數組 –

0

不能使用$ _POST來獲取值。請嘗試:

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

這應該這樣做。

如果請求主體不是標準的urlencoded形式,$ _POST將不會被填充。