1

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]);
?>
1
  • 3
    Missing semi-colon at the end of your JSON string line.. Commented Mar 21, 2014 at 8:47

4 Answers 4

1

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]);
Sign up to request clarification or add additional context in comments.

Comments

0

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'];

Comments

0

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.

Comments

0

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"]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.