PHP:从复杂的JSON对象中获取一个元素

时间:2017-12-12 20:47:33

标签: php json

我试图创建一个交易机器人,此时我应该获得加密货币市场的当前市场价值。

此时我想获得嵌套参数" Last"在这个JSON数组中:

array(3) { ["success"]=> bool(true) 
           ["message"]=> string(0) "" 
           ["result"]=> array(1) { 
                  [0]=> array(13) { ["MarketName"]=> string(8) "USDT-BTC" 
                                    ["High"]=> float(17399.99999999) 
                                    ["Low"]=> float(16166) 
                                    ["Volume"]=> float(7200.55684465)                    
                                    ["Last"]=> float(16486.5864853) 
                                    ["BaseVolume"]=> float(119420929.2692) 
                                    ["TimeStamp"]=> string(21) "2017-12-12T20:36:19.2"                          ["Bid"]=> float(16484)  
                                    ["Ask"]=> float(16486.5864853)                    
                                    ["OpenBuyOrders"]=> int(12858) 
                                    ["OpenSellOrders"]=> int(5529) 
                                    ["PrevDay"]=> float(17369.85623056) 
                                    ["Created"]=> string(23) "2015-12-11T06:31:40.633" 
                                   } 
                                }
             }

我怎样才能得到并展示它?

这是我获取并解码数组的脚本

  private function send($method = null , $args = array() , $secure = true) {
  if(empty($method)) return array("status" => false , "error" => "method was not defined!");

  if($secure) $args["apikey"] = $this->apiKey;
  $args["nonce"] = time();

  $urlParams  = array();
  foreach($args as $key => $val) {
    $urlParams[]  = $key . "=" . $val;
  }

  $uri  = $this->baseUrl . $method;

  $argsString = join("&" , $urlParams);
  if(!empty($urlParams)) {
      $uri  = $uri . "?" . $argsString;
  }

  $sign = $secure == true ? hash_hmac('sha512',$uri,$this->apiSecret) : null;

  $uri = trim(preg_replace('/\s+/', '', $uri));
  $ch = curl_init($uri);
  if($secure) curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $execResult = curl_exec($ch);

  if(curl_error($ch))
  {
      return array("status" => false , "error" => curl_error($ch));
  }

  $obj = json_decode($execResult, true);
  return $obj["result"];

我成功地显示了参数["结果"]:如何在其中显示嵌套的内容? 提前谢谢。

2 个答案:

答案 0 :(得分:2)

与访问初始数组的'result'元素的方式类似,您可以像这样访问嵌套数组中的'last'元素:

$obj['result'][0]['Last'];

您首先要查看$ obj ['result']这是一个数组,然后访问元素0,它也是一个数组。然后,您将访问此阵列上的“last”键。

答案 1 :(得分:-1)

你可以测试$ obj [“result”]是否是一个数组,然后迭代它,然后继续向下钻取,直到找到你想要的东西。

$obj["result"][0]["Last"]  

或者只是尝试直接解决它

`$`('#calendar').fullCalendar({
header: {
    left: 'prev,next',
    center: 'title',
    right: 'month,agendaWeek,agendaDay'
},
buttonText: {
    month: 'month',
    week: 'week',
    day: 'day'
},
events: function (start, end, timezone, callback) {
    contentType: "application/json; charset=utf-8",
    `$`.getJSON({
        url: "WelcomeModels/Calendar/",
        data: { Status: status, BranchNum: branchNumber, FromDate: fDate, ToDate: tDate, calendarMonth: '12', calendarYear: y },
        type: "GET",
        success: function (data) {
            var events = [];
            var obj = data;
            //`$`.map(data, function (me) {
            //    me.title = data['BranchNum'] + ' - BOR - ' + data['Status'], // this is required

                //    me.start = moment(data['ReviewDate']).format();  // this is required

                //    })
                `$`(data).find('event').each(function () {
                    events.push({
                        title: data.BranchNum + ' - BOR - ' + data.Status,
                        start: new Date(data.ReviewDate),
                        backgroundColor: "#0073b7", //Blue
                        borderColor: "#0073b7", //Blue
                        url: 'http://google.com/'
                    });

                });
                callback(events);
            }
        })

    },