我正在尝试安排要存储到数据库中的json数据,即元数据中的电话号码。有人可以帮我吗。谢谢
{
"Body":{
"stkCallback":{
"MerchantRequestID":"26642-2152252-1",
"CheckoutRequestID":"ws_CO_DMZ_240346011_09022019093008828",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{"Name":"Amount","Value":10.00},
{"Name":"MpesaReceiptNumber","Value":"NB92QAMYN2"},
{"Name":"Balance"},
{"Name":"TransactionDate","Value":20190209093032},
{"Name":"PhoneNumber","Value":254723513144}
]
}
}
}
}
我的PHP代码似乎无法正确显示在电话号码上
$PhoneNumber=$json['Body']['stkCallback']["CallbackMetadata"]['Item']['PhoneNumber'];
$sql = "UPDATE pay SET ResultCode='$ResultCode' where uniqs='$PhoneNumber'";
答案 0 :(得分:1)
$data='{
"Body":{
"stkCallback":{
"MerchantRequestID":"26642-2152252-1",
"CheckoutRequestID":"ws_CO_DMZ_240346011_09022019093008828",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{"Name":"Amount","Value":10.00},
{"Name":"MpesaReceiptNumber","Value":"NB92QAMYN2"},
{"Name":"Balance"},
{"Name":"TransactionDate","Value":20190209093032},
{"Name":"PhoneNumber","Value":254723513144}
]
}
}
}
}';
$number=false;
/* use `json_decode` to make the data usable */
$json=json_decode( $data );
/* find the array of items */
$items=$json->Body->stkCallback->CallbackMetadata->Item;
/* loop through the array until you find the one you want. */
foreach( $items as $obj ){
if( $obj->Name=='PhoneNumber' )$number=$obj->Value;
}
/* do something with the number... */
if( $number )echo $number;
在您将数字嵌入sql的问题中-您应该考虑使用prepared statement
来避免sql注入...也许像这样
$number=false;
$json=json_decode( $data );
$items=$json->Body->stkCallback->CallbackMetadata->Item;
$code=$json->Body->stkCallback->ResultCode;
foreach( $items as $obj ){
if( $obj->Name=='PhoneNumber' )$number=$obj->Value;
}
if( $number ){
$sql='update `pay` set `resultcode`=? where `uniqs`=?';
$stmt=$db->prepare($sql);
$stmt->bind_param( 'is', $code, $number );
$stmt->execute();
}
答案 1 :(得分:0)
这是因为$json['Body']['stkCallback']["CallbackMetadata"]['Item']
是一个充满对象的数组(带有Name
和Value
)。
因此,您需要遍历它并获取Name === PhoneNumber
:
foreach ($json['Body']['stkCallback']["CallbackMetadata"]['Item'] as $item) {
if ($item['Name'] === 'PhoneNumber') {
$PhoneNumber = $item['Value'];
}
}
让我知道它是否有效。当然,在将其保存到数据库之前,您应该检查$PhoneNumber
是否真正被填充。
答案 2 :(得分:0)
JSON
对象转换为 PHP Object
。Item
是一个数组,因此需要遍历它以获得所需的字段,即PhoneNumber
。因此,以下是对它的说明-
<?php
$jsonObj = '{
"Body":{
"stkCallback":{
"MerchantRequestID":"26642-2152252-1",
"CheckoutRequestID":"ws_CO_DMZ_240346011_09022019093008828",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{"Name":"Amount","Value":10.00},
{"Name":"MpesaReceiptNumber","Value":"NB92QAMYN2"},
{"Name":"Balance"},
{"Name":"TransactionDate","Value":20190209093032},
{"Name":"PhoneNumber","Value":254723513144}
]
}
}
}
}';
// json_decode your json array here
$phpObj = json_decode($jsonObj, true);
// get items array from the main object
$items = $phpObj['Body']['stkCallback']['CallbackMetadata']['Item'];
// loop over items to get desired field
foreach($items as $obj) {
if($obj['Name'] === 'PhoneNumber')
$PhoneNumber = $obj['Value'];
}
echo $PhoneNumber;
?>