我有一個非常奇怪的問題,我不明白爲什麼。 情況很簡單。我的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也正確解析。
看起來很奇怪 - 你做過'$ accepted'和'$ synced'的'var_dump'來比較嗎? –
是的,我比較他們,var_dump($ accepted,$ synced)給了我結果: NULL 字符串(5)「false」 – andrino
看起來很好:http://codepad.org/JmvUmwBe – AbraCadaver