2017-06-29 99 views
1

我正在使用占星術API發送一個json字符串作爲對查詢的響應。但我一直試圖將其轉換爲json並從響應字符串中檢索不同的項目。下面是響應的例子:獲取json字符串元素

{"ashtakoota":{"status":true,"received_points":26},"manglik":{"status":true,"male_percentage":13.25,"female_percentage":13.75},"rajju_dosha":{"status":false},"vedha_dosha":{"status":false},"conclusion":{"match_report":"Marriage between the prospective bride and groom is highly recommended. The couple would have a long-lasting relationship, which would be filled with happiness and affluence."}} 

我使用PHP腳本,所以我嘗試了下面的代碼:

$json = json_decode($res1, true); TRY 1 --> echo array_values($json[1]); TRY 2 --> echo $json.ashtakoota.status; TRY 3 --> echo $res1.ashtakoota.status;

但產量始終是空白。我懷疑$ json是空的還是json響應不是完美的json。

+1

嘗試4:$ json ['ashtakoota'] ['status'] – YvesLeBorg

+0

非常感謝朋友。有效。 – Vivek

回答

1

PHP爲其數組使用字符串鍵,這是json_decode(...)返回的內容。因此,你需要訪問他們爲:

echo $json['ashtakoota']['status']; 

然後應該爲你的榜樣JSON輸入輸出true

+0

它工作兄弟。非常感謝。它實際上返回1而不是true。 :p – Vivek

1

json_decode上的true參數將導致它返回一個數組而不是對象。對象的語法也不正確,它不是一個點,而是你需要的->

$json = json_decode($res1); 

echo $json->ashtakoota->status; 
+0

它沒有工作的人。但spenderD的答案工作。再次感謝嘗試朋友 – Vivek