KNgu
KNgu

Reputation: 375

JSON PHP Not parsing correctly

I have a JSON I want to decode, but for some reason I can't access the object once I've decoded it.

JSON:

<?php 
$data = '{"error":null,"miningSpeeds":[{"coinTypeCode":"HXC","currentHash":"5 Gh/sec","miningTime":"The last coin switch was 4 minutes ago.","port":"Connect via port 3326 (Keccak)."},{"coinTypeCode":"LOT","currentHash":"9823 Kh/sec","miningTime":"The last coin switch was 44 minutes ago.","port":"Connect via port 3332 (Scrypt)."},{"coinTypeCode":"MZC","currentHash":"11 Th/sec","miningTime":"The last coin switch was 20 minutes ago.","port":"Connect via port 3331 (SHA256)."}]}'

$j = json_decode($data, true);

echo($j['miningSpeeds'][2]);
?>

Upvotes: 1

Views: 69

Answers (4)

Wilmer
Wilmer

Reputation: 2511

You need to provide the key to access the values inside the array:

echo($j['miningSpeeds'][2]["coinTypeCode"]);
echo($j['miningSpeeds'][2]["currentHash"]);
echo($j['miningSpeeds'][2]["miningTime"]);

Upvotes: 0

ajtrichards
ajtrichards

Reputation: 30565

You were missing a semi-colon and you were trying to echo an array - use print_r()

Working Code:

<?php 

$data = '{"error":null,"miningSpeeds":[{"coinTypeCode":"HXC","currentHash":"5 Gh/sec","miningTime":"The last coin switch was 4 minutes ago.","port":"Connect via port 3326 (Keccak)."},{"coinTypeCode":"LOT","currentHash":"9823 Kh/sec","miningTime":"The last coin switch was 44 minutes ago.","port":"Connect via port 3332 (Scrypt)."},{"coinTypeCode":"MZC","currentHash":"11 Th/sec","miningTime":"The last coin switch was 20 minutes ago.","port":"Connect via port 3331 (SHA256)."}]}';

$j = json_decode($data);

$mining_speeds = $j -> miningSpeeds;

echo '<pre>';
    print_r($mining_speeds);

This will print out the Mining Speeds object - you can then access different properties using:

echo 'Coin Type: '.$mining_speeds[2] -> coinTypeCode.'<br/>';
echo 'Hash: '.$mining_speeds[2] -> currentHash.'<br/>';
echo 'Time: '.$mining_speeds[2] -> miningTime.'<br/>';
echo 'Port: '.$mining_speeds[2] -> port.'<br/>';

By changing the number in $mining_speeds will allow you to access different array keys.

Upvotes: 0

nowhere
nowhere

Reputation: 1548

You are missing a semicolon at the end of the json string.

Also you have to tell the echo which field you want to output like:

echo $j['miningSpeeds'][0]['port'];

Upvotes: 0

The $j['miningSpeeds'][2] returns an array and you can't make use of echo to print that . Instead use a print_r

print_r($j['miningSpeeds'][2]);

Also , you missed a semicolon after your JSON data.

The working code..

<?php
$data = '{"error":null,"miningSpeeds":[{"coinTypeCode":"HXC","currentHash":"5 Gh/sec","miningTime":"The last coin switch was 4 minutes ago.","port":"Connect via port 3326 (Keccak)."},{"coinTypeCode":"LOT","currentHash":"9823 Kh/sec","miningTime":"The last coin switch was 44 minutes ago.","port":"Connect via port 3332 (Scrypt)."},{"coinTypeCode":"MZC","currentHash":"11 Th/sec","miningTime":"The last coin switch was 20 minutes ago.","port":"Connect via port 3331 (SHA256)."}]}';
$j = json_decode($data, true);
print_r($j['miningSpeeds'][2]);

Upvotes: 1

Related Questions