2014-02-12 83 views
0

我有一個非常奇怪的問題,我不明白爲什麼。 情況很簡單。我的Android應用上傳JSON數據到我的服務器上的一個PHP腳本。現在我正試圖解析數據。如何在php中訪問JSON對象的JSON屬性?

這是傳遞給腳本的JSON-陣列(經由httpPost.setEntity()):

[{ 「friends_with_accepted」: 「假」, 「friends_with_synced」: 「假」, 「friends_with_second_id」 : 「5」, 「friends_with_first_id」: 「6」}]

這是PHP腳本:

<?php 
// array for JSON response 
$response = array(); 

$json = file_get_contents ('php://input'); 
$jsonArray = json_decode ($json, true); 
foreach ($jsonArray as $jsonObject) { 
    $firstId = $jsonObject['friends_with_first_id']; 
    $accepted = $jsonObject ['friends_with_accepted']; 
    $secondId = $jsonObject ['friends_with_second_id']; 
    $synced = $jsonObject ['friends_with_synced']; 

    echo "accepted: ".$accepted."synced: ".$synced; 
} ?> 

這是我從劇本得到的迴應:

接受:同步:假

爲什麼是「同步」屬性正確傳遞,而不是「接受」財產? 我看不出差異。順便說一句,firstId和secondId也正確解析。

+0

看起來很奇怪 - 你做過'$ accepted'和'$ synced'的'var_dump'來比較嗎? –

+0

是的,我比較他們,var_dump($ accepted,$ synced)給了我結果: NULL 字符串(5)「false」 – andrino

+0

看起來很好:http://codepad.org/JmvUmwBe – AbraCadaver

回答

1

好吧,我只是發現了問題:

而不是

$accepted = $jsonObject ['friends_with_accepted']; 

我刪除的JSONObject之間和支架

$accepted = $jsonObject['friends_with_accepted']; 

我也絕對不知道,爲什麼它的工作空間爲其他屬性,但這對我來說並不重要。

+0

必須是版本差異: http://codepad.org/JmvUmwBe – AbraCadaver