donglu1973 2015-12-10 22:30 采纳率: 100%
浏览 182
已采纳

如何从多维json数组中获取值

i'm entirely new to this site, so i'm sorry in advance if my post is not formatted properly.

Anyway, i have what i expect to be a fairly simple question. I'm extracting values from a "request body"-array-thingy, and i got most of what i need by using these 4 lines of code:

$payment_id = strval($callback_json->id);
$order_id = strval($callback_json->order_id);
$currency = strval($callback_json->currency);
$card_brand = strval($callback_json->metadata->brand);

My problem now is that i've run out talent when trying to get the "amount" value that seems to be a "sub-variable" to "operations".

I've tried doing it like this, but neither of them work:

$amount_total = strval($callback_json->operations[amount]);
$amount_total = strval($callback_json->operations->amount);

So my question now is; How do i format this line to get the value "69500".

I really hope someone out there can help me! :-)

{
    "id":9256797,
    "order_id":"23322651466",
    "accepted":true,
    "type":"Payment",
    "text_on_statement":null,
    "branding_id":null,
    "variables":{},
    "currency":"DKK",
    "state":"new",
    "operations":[{
        "id":1,
        "type":"authorize",
        "amount":69500,
        "pending":false,
        "qp_status_code":"20000",
        "qp_status_msg":"Approved",
        "aq_status_code":"20000",
        "aq_status_msg":"Approved",
        "data":{},
        "callback_url":"http://requestb.in/105y8k81",
        "callback_success":null,
        "callback_response_code":null,
        "created_at":"2015-12-05T12:40:40+00:00"
        }],

"metadata":{
    "type":"card",
    "brand":"visa",
    "last4":"0008",
    "exp_month":11,
    "exp_year":2016,
    "country":"DNK",
    "is_3d_secure":false,
    "hash":"6f976a4e388928beb4ad3OrQHCS2LDGNAFZVK3i54p6q8heV0RRci",
    "number":null,
    "customer_ip":"2.110.77.40",
    "customer_country":"DK",
    "fraud_suspected":false,
    "fraud_remarks":[]
}
  • 写回答

1条回答 默认 最新

  • dongnuo6310 2015-12-10 22:40
    关注

    use

    $amount_total = strval($callback_json->operations[0]->amount);
    

    because [ in json is an open tag for an array.

    {'foo':[{'bar':"A"},{'bar':"B"}]}
    $val->foo[0]->bar;  # A
    $val->foo[1]->bar;  # B
    

    Hope that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用