How to remove some data from a JSON and display only the rest?

Asked

Viewed 453 times

0

Hello, I want to print some data from a JSON page in PHP, for example:

I got this page here http://hts03.kshost.com.br:8642/statistics? json=1

JSON:

{
    "totalstreams": 1,
    "activestreams": 1,
    "currentlisteners": 19,
    "peaklisteners": 57,
    "maxlisteners": 500,
    "uniquelisteners": 18,
    "averagetime": 881,
    "version": "2.2.1.109 (posix(linux x64))",
    "streams": [{
        "id": 1,
        "currentlisteners": 19,
        "peaklisteners": 57,
        "maxlisteners": 500,
        "uniquelisteners": 18,
        "averagetime": 881,
        "servergenre": "� s� granada na cara da concorrencia",
        "serverurl": "http:\/\/www.habbonados.com.br",
        "servertitle": "gabrielsa441",
        "songtitle": "Wiz Khalifa - See You Again ft. Charlie Puth [Official Video] Furious 7 Soundtrack",
        "irc": "https:\/\/www.habbo.com.br\/hotel?room=81601237",
        "icq": "NA",
        "aim": "NA",
        "streamhits": 2493,
        "streamstatus": 1,
        "backupstatus": 0,
        "streampath": "\/",
        "streamuptime": 645,
        "bitrate": 64,
        "content": "audio\/aacp"
    }]
}

I want to get that data out of her:

"uniquelisteners":10,
"averagetime":462,
"servergenre":"� s� granada na cara da concorrencia"

And move to a PHP page with echo

  • 1

    Please put the external link data in the question. With the formatação.

2 answers

5

Look do so, there was a problem in the Character encoding, we have to do utf8_encoding:

<?php
$jsonRaw = file_get_contents('http://hts03.kshost.com.br:8642/statistics?json=1');
$jsonParsed = json_decode(utf8_encode($jsonRaw), true);

echo($jsonParsed['uniquelisteners']). '<br>';
echo($jsonParsed['averagetime']). '<br>';
echo($jsonParsed['streams'][0]['servergenre']). '<br>';

Here’s a solution using Curl:

<?php
function get_data($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
}

$jsonRaw = get_data('http://hts03.kshost.com.br:8642/statistics?json=1');
$jsonParsed = json_decode(utf8_encode($jsonRaw), true);

echo($jsonParsed['uniquelisteners']). '<br>';
echo($jsonParsed['averagetime']). '<br>';
echo($jsonParsed['streams'][0]['servergenre']). '<br>';
  • It worked, thank you very much!

  • you must decide which answer is right.

1


The most direct way would be this:

<?php
$url = 'http://hts03.kshost.com.br:8642/statistics?json=1';
$dados = json_decode(file_get_contents($url));

Now the "$data" variable has all the JSON information, you write as you like on the screen.

  • A blank page appears, var_dump($dados) = Null

  • How do I write as I want on the page? For example print only one data

  • Filippesims, take a look at @Miguel’s reply

Browser other questions tagged

You are not signed in. Login or sign up in order to post.