2017-06-29 69 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