我正在向远程服务器发送请求,它正在发送纯文本JSON类型响应!我尝试在php变量中获取该响应并尝试使用json_decode,但它总是返回null值!
<?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);
?>
问题:preg_replace在已引用的文本之间添加引用!
PS:这就是我想通过php数组http://i.imgur.com/4ZGEHAv.png
将数据打印到表中的方法您可以在上面的屏幕截图中使用原始网站的Diverted Train响应!
答案 0 :(得分:1)
我应该告诉你将json转换为php变量的基本语法。请记住,这是使用utf-8编码
//让这个例子清楚明白
$json = {"foo-bar" : 1234};
$obj = json_decode($json);
print $obj -> {'foo-bar'}; // 1234
如果你想把它作为php var
$var = $obj->{'foo-bar'}; // the values keep 1234
将json转换为php array assoc
如果你使用像json这样的
,你不能使用json来获取php var$json = {'foo' : '1234'}; // return null
$json = {foo : 1234}; // Null
$json = {foo: "1234"};
该示例可能在javascript中有效,但在json中无效。
答案 1 :(得分:0)
我能够通过以下修改来解析它:
obj1431027525490 =
;
trnName
工作示例:
<?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);
答案 2 :(得分: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);
?>