drls2738 2015-03-14 04:25
浏览 7

如何将json响应转换为php对象?

Below is my json response, I am using json_decode for converting the response in php object, 'status' and 'msg' is converted successfully but problem is with 'amt'. How to get 'amt' in php object? $amt=$response->transaction_details['amt']; is not working.

RESPONSE :

{"status":1,"msg":"Transactions Fetched Successfully","transaction_details":{"686868686505":{"payid":"293642892","amt":"10.00","txnid":"686868686505","additional_charges":"0.00","productinfo":"SHIRT","firstname":"WILLIAM}}}

CODE :

$response = json_decode($o,true);
$msg = $response['msg']; 
$status = $response['status'];
$amt=$response->transaction_details->['amt'];
echo '<br>';
echo $amt;
echo '<br>';
echo $msg;
echo '<br>';
echo $status;

OUTPUT :

Transactions Fetched Successfully
1

  • 写回答

1条回答 默认 最新

  • dongmi1864 2015-03-14 05:42
    关注

    you are mixing arrays with object.

    json_decode second parameter will let you to get json object or json array, it will return json object if it is null or false, and return json array if you set the second parameter to true or anything that translate to true.

    see the documentation of it in php.net

    that so, you need to change it to:

    $response = json_decode($o); // or json_decode($o, false);
    $msg = $response->msg; 
    $status = $response->status;
    //since you use all numbers for json property, I'll assume this would fail
    $amt=$response->transaction_details->686868686505->amt;
    echo '<br>';
    echo $amt;
    echo '<br>';
    echo $msg;
    echo '<br>';
    echo $status;
    

    or if you like using array

    $response = json_decode($o,true);
    $msg = $response['msg']; 
    $status = $response['status'];
    $amt=$response['transaction_details']['686868686505']['amt'];
    echo '<br>';
    echo $amt;
    echo '<br>';
    echo $msg;
    echo '<br>';
    echo $status;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类