首页 > 解决方案 > PHP中的JSON数据提取

问题描述

JSON data在服务器中有以下内容。我如何只能提取“ SUCCESS”作为输出PHP,其中SUCCESS的结果Status在哪里objectdata“状态”:“成功”,

{  
   "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;

$output给了我上面的 JSON。但是当我使用你的答案时,我的输出是空的。有什么我做错了吗?

标签: phpjsonserver

解决方案


希望这对你有用:

用于json_decodejson 字符串

$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


推荐阅读