[size=24px][size=18px]如何获取json格式数据中某个指定字段的值?
如下json格式的数据:
{"returnCode":"0","resultCode":"0","sign":"19333CD7F9710A104DA5D815709697D2","outChannelNo":"2017120100401000000017","status":"02","mchId":"000000010000000002","channel":"wxPubQR","body":"收单支付","outTradeNo":"20171201150337579753","amount":0.01,"transTime":"20171201150337"}
我要直接获取body字段的值,如何不用json_decode的方法得到“”收单支付“”

解决方案 »

  1.   

    json_decode就是最简单的方法
      

  2.   

    建议用json_decode也可用正则
    $re = '/"body":"([^"]+)"/';
    $str = '{"returnCode":"0","resultCode":"0","sign":"19333CD7F9710A104DA5D815709697D2","outChannelNo":"2017120100401000000017","status":"02","mchId":"000000010000000002","channel":"wxPubQR","body":"收单支付","outTradeNo":"20171201150337579753","amount":0.01,"transTime":"20171201150337"}';
    preg_match_all($re, $str, $matches);
    print_r($matches[1]);
      

  3.   

    $re = '/"body":"([^"]+)",/';
    $str = '{"returnCode":"0","resultCode":"0","sign":"19333CD7F9710A104DA5D815709697D2","outChannelNo":"2017120100401000000017","status":"02","mchId":"000000010000000002","channel":"wxPubQR","body":"收单支付","outTradeNo":"20171201150337579753","amount":0.01,"transTime":"20171201150337"}';
    preg_match_all($re, $str, $matches[1]);
    print_r($matches);
      

  4.   

    正则
    切割
    stripos
    ....方法很多不过有点『做』
      

  5.   

    json_decode  把他转出数组  然后 直接获取 就可以了 
      

  6.   

    json_decode简单方便快捷