Jump to content

Help with Json


Sunnyisles
 Share

Go to solution Solved by Phi11W,

Recommended Posts

Hi
Can someone help me with this code?

<?php
//DOLLAR//
$json_dolar=llamar_api("https://mercados.ambito.com//dolar/informal/variacion");
        $dolar=$json_dolar;
        $compra=$dolar->compra;
        $venta=$dolar->venta;
        $variacion=$dolar->variacion;
        $fecha=$dolar->fecha;
        $var2=$dolar->class-variacion;
?>

This is OK
<?php echo ($compra); ?>

This is Not, probably 'cause de Hyphen
<?php echo ($class-variacion); ?>

Thanks

Link to comment
Share on other sites

First, please use the code icon (<>) in the menu and specify PHP for your code. You don't say what error your are getting but that syntax is probably wrong. You don't show how $class is defined but as a guess given almost no information from you I'd say you want this:

<?php echo ($class->variacion); ?>

 

Link to comment
Share on other sites

5 hours ago, Sunnyisles said:

I just need to echo class-variacion from this url https://mercados.ambito.com//dolar/informal/variacion

This doesn't use your api but if page is always formatted that way then possibly
 

 <?php
//DOLLAR//
    $json_dolar=file_get_contents("https://mercados.ambito.com//dolar/informal/variacion");
   
   $dolar=explode("\",", substr_replace(trim($json_dolar, '{}') ,"",-1));
        
	echo trim(explode("\":\"", $dolar[4])[1])."<br><br>";
	
	foreach($dolar as $key => $value){
		echo $value." OR ".trim(explode("\":\"", $dolar[$key])[1])."<br>";
	}

?>

Which displays

 

up

"compra":"202,50 OR 202,50
"venta":"206,50 OR 206,50
"fecha":"04\/01\/2022 - 16:45 OR 04\/01\/2022 - 16:45
"variacion":"0,24% OR 0,24%
"class-variacion":"up OR up

 

Edited by dodgeitorelse3
Link to comment
Share on other sites

  • Solution
15 hours ago, Sunnyisles said:

$var2=$dolar->class-variacion;

You can work with hyphenated attribute names, but you have to wrap them up a bit more:  

$var2=$dolar->{'class-variacion'};

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.