简体   繁体   English

如何解码一个json对象

[英]How to decode a json object

This might be simple to some but a challenge to me.这对某些人来说可能很简单,但对我来说却是一个挑战。 Well from a documentation, I am getting JSON object in the body of the POST request.从文档中,我在 POST 请求的正文中获取了 JSON 对象。 The format is shown below.格式如下所示。

Question how do I decode the json object when they have not given the variable name of the json object.问我如何在没有给出 json 对象的变量名称时解码 json 对象。 I thought I would get a variable name so that I would decode this way $data = json_decode($jsonFromSource);我想我会得到一个变量名,这样我就可以这样解码$data = json_decode($jsonFromSource);

I can not figure out a way to do the same, anyone?我想不出一种方法来做同样的事情,有人吗? Thanks.谢谢。

Direction from documentation: The POST request will be sent to a URL provided by the enterprise.文档说明:POST 请求将发送到企业提供的 URL。 The following POST parameters that will be passed will be as follows下面将传递的POST参数如下
The parameters passed above will be contained in a JSON object in the body of the POST request.上面传递的参数将包含在 POST 请求正文中的 JSON 对象中。 Link to the brief documentation https://app.kopokopo.com/push_api链接到简要文档https://app.kopokopo.com/push_api

{
   "service_name" : "MPESA",
   "business_number" : "888555",
   "transaction_reference" : "DE45GK45"
   "internal_transaction_id" : 3222
   "transaction_timestamp" : "2013-03-18T13:57:00Z"
   "transaction_type" : "Paybill"
   "account_number" : "445534"
   "sender_phone" : "+254903119111"
   "first_name" : "John"
   "middle_name" : "K"
   "last_name" : "Doe"
   "amount" : 4000
   "currency" : "KES"
   "signature" : "dfafwerewKkladaHOKJdafdf"
}

I am not sure how you are initially retrieving the data - perhaps this will help?我不确定您最初是如何检索数据的 - 也许这会有所帮助?

$url='http://www.example.com/api/feed.json';
$data=file_get_contents( $url );
$json=json_decode( $data,true );
foreach( $json as $param => $value ) echo $param, $value, '<br />';

KOPOKOPO will push below payload to you which is not JSON KOPOKOPO 会将以下有效载荷推送给您,这不是 JSON

"service_name=M-PESA&business_number=888555&transaction_reference=DE45GK45&internal_transaction_id=3222&transaction_timestamp=2013-03-18T13%3A57%3A00Z&transaction_type=Paybill&account_number=445=K.&last_name=Doe&amount=4000&currency=KES"

You could then encode like this :然后你可以这样编码:

$callbackJSONData = json_encode($_POST);

Result will be ...结果将是...

{"service_name":"M-PESA","business_number":"888555","transaction_reference":"DE45GK45","internal_transaction_id":"3222","transaction_timestamp":"2013-03-18T13:57:00Z","transaction_type":"Paybill","account_number":"445=K.","last_name":"Doe","amount":"4000","currency":"KES"}

$jdata = json_decode($post,true);

   /*
   * We get details from $_POST only
   */
   $first_name = $jdata["first_name"];
   $middle_name = $jdata["middle_name"];
   $last_name = $jdata["last_name"];
   $paid_amount       = $jdata["amount"];
   $paying_phone_number = str_replace('+254', '0', preg_replace('/[^0-9+]/', '', $jdata["sender_phone"]));
   $transactionID       = $jdata["internal_transaction_id"];
   $transactionrefID        = str_replace('-','',$jdata["service_name"]);
   $MpesaCode = $jdata["transaction_reference"];
   $providerCurrency = $jdata["currency"];
   $providerSignature = $jdata["signature"];
   $payment_date = date("Y-m-d H:i:s",strtotime($jdata["transaction_timestamp"])); 
   $payment_time_at = date("Y-M-d",strtotime(NOW)) .' at '.date("H:i A",strtotime(NOW));
   $payment_type = $jdata['transaction_type'];
   $payment_destination = $jdata['business_number'];

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

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