0

I am sending request to remote server and it is sending plain text JSON type response! I tried to take that response in php variable and tried for json_decode but it always 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 already quoted text!

PS: This is how i want to print the data into table via php array https://i.sstatic.net/Pf0Ek.png

You can use Diverted Train response from original website in above screenshot!

9
  • 1
    the pasted response is not valid json. Thats the reason for returning null. There are no quotes and there are inline functions. Commented May 8, 2015 at 5:04
  • yeah, even I felt that there are no quotes. But then how is enquiry.indianrail.gov.in/ntes showing the response? I tried code from original site (javascript not php) pastebin.com/usBpmFKv Commented May 8, 2015 at 5:09
  • the obj1431027525490 = should not be a part of your json definition. And I don't belive that the inline functions should work in PHP. Commented May 8, 2015 at 5:09
  • they execute it in javascript. As you can see in the inline functions, you need at least the _LANG variable to be defined locally. This is pure JS. This will not work in php. Commented May 8, 2015 at 5:11
  • we can remove obj1431027525490 = by substr! Any idea how to add quotes to remaining part? and also use EN value of that JS function Commented May 8, 2015 at 5:12

3 Answers 3

1

I should to tell you basic syntax to convert json to php variable. keep in mind, This is onli work with utf-8 encoding

// let the example yo be clear to understand

$json = {"foo-bar" : 1234};

$obj = json_decode($json);

print $obj -> {'foo-bar'}; // 1234

If you want to get it as php var

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

It turn json to php array assoc

You cannot use json to get in php var if you use json like

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

The example maybe valid in javascript, but not in json.

Sign up to request clarification or add additional context in comments.

2 Comments

So dude got any idea? I tried this also: stackoverflow.com/questions/8815586/…
If it there some double quotes, will converted as string object, they tell the true, any question ?
0

I was able to parse it with the following modifications:

  • removed the obj1431027525490 =
  • removed the ; at the end
  • removed the inline functions with key trnName
  • sourrounded all keys with double quotes

working example:

<?php
$json=<<<EOT
{ "trains": [ { "trainNo": "12392", "startDate": "6 May 2015", "trainName": "SHRAMJEEVI EXPRESS", "divertedFrom": "NDLS", "divertedTo": "GZB", "trainSrc": "NDLS", "trainDstn": "RGD", "trainType": "SUPERFAST" }, { "trainNo": "13162", "startDate": "7 May 2015", "trainName": "BLGT-KOLKATA EXP.", "divertedFrom": "NFK", "divertedTo": "KOAA", "trainSrc": "BLGT", "trainDstn": "KOAA", "trainType": "MAIL_EXP" }] }
EOT;

$contents = utf8_encode($json); 
$obj = json_decode($contents,true);
var_dump($obj);

3 Comments

Yeah, I did it too! But how to remove inline function and surround all key without human intervention! I am trying stringfyjs! Have a look t o it
To Add double quotes: $json = preg_replace('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/', '"$1"', $json);
I made the php-function which would replace the javascript function with the value! Now issue left is of adding quotes!
0
<?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;
}
function json_fix_quotes ($string){
    //$string = str_replace("{",'{"',$string);
    $string = str_replace(":'",'":"',$string);
    $string = str_replace(': ','": ',$string);
    $string = str_replace(", ",', "',$string);
    $string = str_replace("',",'","',$string);
    $string= str_replace('{" ','{"',$string);
    $string= str_replace(', "{',', {',$string);
    $string = str_replace("{ ",'{"',$string);
    return $string;
}

$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 = json_fix_quotes($json);
echo $json;
$contents = utf8_encode($json); 
$obj = json_decode($json,true);
var_dump($obj);

?>

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.