简体   繁体   English

PHP JSON解析响应

[英]PHP JSON parse response

Using loop I want to get the following paramatersid,device_id from the given code 使用循环我想从给定的代码中获取以下paramatersid,device_id

$str = '{"response":{"success":true,"result":{"success":[{"id":"31281985","device_id":"26965","message":"BIG WINTER CLOSING SALE! Starting from FRIDAY 30th Dec 2016. Discount upto 55%OFF on all Ladies, Gents & Kids Shoes. at All Branches of ENGLISH SHOES MULTAN. ","status":"pending","send_at":1485859669,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1485863269,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1485859669,"contact":{"id":"6317522","name":"923456812536","number":"923456812536"}},{"id":"31281984","device_id":"26965","message":"BIG WINTER CLOSING SALE! Starting from FRIDAY 30th Dec 2016. Discount upto 55%OFF on all Ladies, Gents & Kids Shoes. at All Branches of ENGLISH SHOES MULTAN. ","status":"pending","send_at":1485859669,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1485863269,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1485859669,"contact":{"id":"6317521","name":"923336088811","number":"923336088811"}}],"fails":[]}},"status":200}'

please Guide how to proceed . 请指导如何进行。 it seems its nested json response 似乎其嵌套的json响应

$arr = json_decode($str);    // decode json result 
$results = [];

foreach($arr->response->result->success as $result) {
  $results[] = [
    'paramatersid' => $result->id,
    'deviceid' => $result->device_id
  ];
}
?>

Use this custom php code 使用此自定义php代码

<?php

$str = '{
"response": {
    "success": true,
    "result": {
        "success": [{
            "id": "31281087",
            "device_id": "26965",
            "message": "BIG WINTER CLOSING SALE! Starting from FRIDAY 30th Dec 2016. Discount upto 55%OFF on all Ladies, Gents & Kids Shoes. at All Branches of ENGLISH SHOES MULTAN. ",
            "status": "pending",
            "send_at": 1485858313,
            "queued_at": 0,
            "sent_at": 0,
            "delivered_at": 0,
            "expires_at": 1485861913,
            "canceled_at": 0,
            "failed_at": 0,
            "received_at": 0,
            "error": "",
            "created_at": 1485858313,
            "contact": {
                "id": "6317513",
                "name": "0",
                "number": "0"
            }
        }],
        "fails": []
    }
},
"status": 200

}' ; }';

$arr = json_decode($str);    // decode json result 

//echo "<pre>";
//print_r($arr);   // print $arr value parsed from json

$paramatersid = $arr->response->result->success[0]->id;    // getting id 

$divice_id = $arr->response->result->success[0]->device_id;  // getting device id 

echo $paramatersid ."<br>".$divice_id ;

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM