How to Covert this text json response into php array -


i sending request remote server , sending plain text json type response! tried take response in php variable , tried json_decode returns null value!

<?php  function removefunction($data){ checkagain: $functionposition=stripos($data,"function()"); if($functionposition){ $subdata= substr($data, $functionposition); $functiondata=substr($data, $functionposition,stripos($subdata,"}")+1);  $endoffunction=stripos($subdata,"}"); $endoffunction=$endoffunction+$functionposition;  $questionmarkpos=stripos($functiondata,'?"'); $colonpos=stripos($functiondata,'":"'); $realvalue=substr($functiondata, $questionmarkpos+2,$colonpos-$questionmarkpos-2); $data=str_ireplace($functiondata,"\"$realvalue\"",$data); goto checkagain; } return $data; }  $json=<<<eot obj1431027525490 = { trains: [ { trainno: "12392", startdate: "6 may 2015", trainname: "shramjeevi express", trnname:function(){return _lang=="en-us"?"shramjeevi express":""}, divertedfrom: "ndls", divertedto: "gzb", trainsrc: "ndls", traindstn: "rgd", traintype: "superfast" }, { trainno: "13162", startdate: "7 may 2015", trainname: "blgt-kolkata exp.", trnname:function(){return _lang=="en-us"?"blgt-kolkata exp.":""}, divertedfrom: "nfk", divertedto: "koaa", trainsrc: "blgt", traindstn: "koaa", traintype: "mail_exp" }] };  eot; $json= substr($json, 19); $json=substr_replace($json, "", -2); echo $json."<br/><br/><br/>".php_eol.php_eol.php_eol; $json=removefunction($json); $json = preg_replace('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/', '"$1"', $json); echo $json; $contents = utf8_encode($json);  $obj = json_decode($json,true); var_dump($obj);  ?> 

issue: preg_replace adds quote between quoted text!

ps: how want print data table via php array http://i.imgur.com/4zgehav.png

you can use diverted train response original website in above screenshot!

i should tell basic syntax convert json php variable. keep in mind, onli work utf-8 encoding

// let example yo clear understand

$json = {"foo-bar" : 1234};  $obj = json_decode($json);  print $obj -> {'foo-bar'}; // 1234 

if want php var

$var = $obj->{'foo-bar'}; // values keep 1234 

it turn json php array assoc

you cannot use json in php var if use json

$json = {'foo' : '1234'};  // return null $json = {foo : 1234}; // null $json = {foo: "1234"};  

the example maybe valid in javascript, not in json.


Comments