1

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?.

1
  • What do you get when print_r($jsoni);? Commented Nov 6, 2014 at 9:35

2 Answers 2

1

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?

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

4 Comments

json_decode second arg defaults to false, what means that you get an array, not an object. The json is valid, by the way, I tested.
are you sure? default is object.
thanks i've done with this $routes_comp=preg_replace('/\s+/', '',$routes_comp); $json=json_decode(stripslashes($routes_comp)); $os=$json->OrderStatus;
Indeed, I just saw it, whitespaces may cause problems, weird that I tested the code with whitespaces and it worked, actually
1

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.