nishant
nishant

Reputation: 489

return value is showing as string in place of json

I am using file get content method to get stats data as below

$val = file_get_contents('https://example.com/getStatsData');

I am getting output value as below

{"msg":"success","data":[[{"stats_abc":"1223","stats_bcd":"55684","stats_cda":"14999","stats_def":232456,"stats_efg":"432868","stats_fgh":"7558","stats_ghi":"3,778.72","stats_date":"20-01-2018 17:00 PM"}],null]}

But when I am decoding it as json_decode($val). It is not showing anything. Can anyone help me to get above data as array/object?

Thanks in advance

Upvotes: 0

Views: 46

Answers (3)

prakash tank
prakash tank

Reputation: 1267

Please try this : i have put you response in local json file and call and it's working fine. check this :

<?php
       $val= file_get_contents('http://localhost/j/test.json');
       print_r(json_decode($val,true));
?>

Upvotes: 1

KIKO Software
KIKO Software

Reputation: 16688

This does work:

<?php

$json = '{
  "msg": "success",
  "data": [
    [{
      "stats_abc": "1223",
      "stats_bcd": "55684",
      "stats_cda": "14999",
      "stats_def": 232456,
      "stats_efg": "432868",
      "stats_fgh": "7558",
      "stats_ghi": "3,778.72",
      "stats_date": "20-01-2018 17:00 PM"
    }], null
  ]
}';

echo '<pre>';
print_r(json_decode($json));
echo '</pre>';

and results in:

stdClass Object
(
    [msg] => success
    [data] => Array
        (
            [0] => Array
                (
                    [0] => stdClass Object
                        (
                            [stats_abc] => 1223
                            [stats_bcd] => 55684
                            [stats_cda] => 14999
                            [stats_def] => 232456
                            [stats_efg] => 432868
                            [stats_fgh] => 7558
                            [stats_ghi] => 3,778.72
                            [stats_date] => 20-01-2018 17:00 PM
                        )
                )
            [1] => 
        )
)

just to show that the json itself is fine.

Upvotes: 1

Punit Gupta
Punit Gupta

Reputation: 4388

First make sure, $val is not empty. Then use json_decode($val, 1). This will convert it to associative array, as expected.

Upvotes: 0

Related Questions