Reputation: 1084
i have a response encoded in JSON. Stored in a variable and trying to decode. But it not displaying any thing. code is here
echo $response; //prints response like
({ "OrderStatus" : "REJECT", "OrderID" : "","PONumber" :"", "Reject Reason" : "INVALID_FLD_VALUE You have entered an Invalid Field Value 41111 for the following field: ccnumber", "AUTHCODE" : "", "ShippingCost" : "", "HandlingCost" : ""});
//Trimming braces etc
$routes = ltrim($response, '('); //left trim
$routes_comp= rtrim($routes, ');'); //right trim
echo "<br/>";
echo $routes_comp;
//decoding here
$jsoni=json_decode($routes_comp);
$var= $jsoni->OrderStatus;
print_r($var);
exit;
i want order status value but its not displaying any thing.what is the actual way?.
Upvotes: 1
Views: 93
Reputation: 1084
Done with this tech. actually the error was white spaces between json string words.
$routes_comp=preg_replace('/\s+/', '',$routes_comp);
$json=json_decode(stripslashes($routes_comp));
$os=$json->OrderStatus;
Upvotes: 1
Reputation: 1288
use json_decode($routes_comp, true); -- Wrong
EDIT Tested your code creating response variable as you said it was;
$response = '({ "OrderStatus" : "REJECT", "OrderID" : "","PONumber" :"", "Reject Reason" : "INVALID_FLD_VALUE You have entered an Invalid Field Value 41111 for the following field: ccnumber", "AUTHCODE" : "", "ShippingCost" : "", "HandlingCost" : ""});';
$routes = ltrim($response, '('); //left trim
$routes_comp= rtrim($routes, ');'); //right trim
echo "<br/>";
echo $routes_comp;
//decoding here
$jsoni=json_decode($routes_comp);
$var= $jsoni->OrderStatus;
print_r($var);
exit;
Got This
<br/>{ "OrderStatus" : "REJECT", "OrderID" : "","PONumber" :"", "Reject Reason" : "INVALID_FLD_VALUE You have entered an Invalid Field Value 41111 for the following field: ccnumber", "AUTHCODE" : "", "ShippingCost" : "", "HandlingCost" : ""}REJECT
So I think $response may be coprrupted somehow. Could you put var_dump before trimming and test again?
Upvotes: 1