简体   繁体   中英

Parse json_decode php

I've read a lot of the json_decode questions, tried a variety of different things I can't this to work.

it's the bittrex api

$apikey='4058';
$apisecret='50860';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/public/getticker?    apikey='.$apikey.'&nonce='.$nonce.'&market=BTC-LTC';
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$json = json_decode($execResult,true);

I do get an output, now I just want the "Last" value

{"success":true,"message":"","result":{"Bid":0.00002130,"Ask":0.00003341,"Last":0.00002121}}

I've tried for each

foreach($json->result as $market) {
 $lastPrice = $market->Last;

//this was to see if it was an echo problems, tried storing the last price in the db... I get null
 $collectionGamerActions->update(array('gamer'=>$gamer),
    array('$set'=>array('lastPrice'=>$lastPrice,'reached'=>1)));
 //print "last price is $lastPrice";
}

I tried

$lastPrice = $json->result->Last;

and a variety of

 $lastPrice = $json[0]['result']['last']

I gave up on php and tried javascript, 1st stringifying he response, then parsing it

var obj = JSON.stringify(response);
var obj =JSON.parse(obj);
console.log("last is " + obj.result.Last + " obj is " + obj);

nothing works... can I get direction in what I'm doing wrong please.

Tried some of the suggestions below

    var obj = JSON.stringify(response);
    var json = JSON.parse(response);
    console.log(json.result.Last);

on the php side the response is generated here

   $execResult = curl_exec($ch);
   $json = json_decode($execResult);

   echo json_decode($json, true);

results in javascript error SyntaxError: Unexpected number var json = JSON.parse(response);

With the php suggestions

 $json = json_decode($execResult);

 $json = json_decode($json, true);

 var_dump($json['result']['Last']);

results in NULL

As you use (note the second parameter in json_decode ):

$json = json_decode($execResult,true);

You will have an associative array, so your value will be in:

$json['result']['last']

Note that $json->result->Last would work if you use json_decode() without the second parameter (the default value, false ).

I may be misunderstanding your problem, but... assuming PHP is giving you the string you pasted above, then you can just use JSON.parse (no need for stringify) and grab the result from the resulting object.

var string = '{"success":true,"message":"","result":{"Bid":0.00002130,"Ask":0.00003341,"Last":0.00002121}}';
var json = JSON.parse(string);
console.log(json.result.Last); // 0.00002121

Array notation (second argument to json_decode() is true ):

$string = '{"success":true,"message":"","result": {"Bid":0.00002130,"Ask":0.00003341,"Last":0.00002121}}';

$json = json_decode($string, true);

var_dump($json['result']['Last']);

Object notation (second argument to json_decode() is false ):

$string = '{"success":true,"message":"","result":{"Bid":0.00002130,"Ask":0.00003341,"Last":0.00002121}}';

$json = json_decode($string);

var_dump($json->result->Last);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM