短信响应字符串迭代

时间:2017-03-21 06:16:09

标签: php arrays json string

我有字符串接收如下,这不是一个合适的json字符串。

{"recipients": 1, "delivery_stats": {"delivered": 0, "bounced": 0, "responses": 0, "pending": 1, "optouts": 0}, "sms": 1, "cost": 1, "send_at": "2017-03-21 05:04:01", "error": {"code": "SUCCESS", "description": "OK"}, "message_id": 9579849}

我需要提取数据以检查某些参数,例如代码等于SUCCESS,并获取send_atmessage_id的值。

PHP中最好的方法是什么?它可以转换为json字符串并迭代吗?

1 个答案:

答案 0 :(得分:2)

这是一个json字符串。 Demo

<?php
$o = json_decode('{"recipients": 1, "delivery_stats": {"delivered": 0, "bounced": 0, "responses": 0, "pending": 1, "optouts": 0}, "sms": 1, "cost": 1, "send_at": "2017-03-21 05:04:01", "error": {"code": "SUCCESS", "description": "OK"}, "message_id": 9579849}', true);

echo $o['error']['code'];