PHP中的JSON数据提取

时间:2018-05-03 06:25:26

标签: php json server

我在服务器中有以下JSON data。我如何仅在SUCCESS中提取“PHP”作为输出,其中SUCCESSStatus的{​​{1}}内object的结果。即“status”:“SUCCESS”,

data

更新

{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}

这个$ch = curl_init(); // initiate curl curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); // tell curl you want to post something curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string); // define what you want to post curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $output = curl_exec ($ch); // execute //echo "Request_Json= ".$data_string; //echo "Response_Json=".$output; echo $output; 给了我上面的JSON。 但是当我使用你的答案时,我得到输出为空。我做错了什么?

2 个答案:

答案 0 :(得分:1)

希望这对您有用:

json_decode用于json字符串

$json= '{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}';


$arr = json_decode($json);

//print_r($arr);
echo $arr->response->data->status;
/*Output SUCCESS*/
die;

更新:

 $output = curl_exec ($ch); 
 $arr = json_decode($output );
 echo $arr->response->data->status;

了解更多:http://php.net/manual/en/function.json-decode.php

答案 1 :(得分:0)

$output = '{  
   "response":{  
      "status":true,
      "statusCode":"0",
      "statusDescription":"Amount Debited",
      "data":{  
         "balanceamount":"528",
         "status":"SUCCESS",
         "statuscode":"0",
         "statusdescription":"Amount Debited",
         "debitedamount":"2",
         "orderid":"hj",
         "refId":"4450"
      }
   },
   "checksum":"23e97234820f5987189245e4216e89425472c2fe2c957749743b21d828efae67"
}';
 $json_result = json_decode($output);
echo $json_result->response->data->status;