0

I need return json object from php, this is my code:

      $resultFromDB = PerformTransaction($requestToDB);
      if($resultFromDB['status'] == '0'){
        $myReturn = array('status' => '0','message' => 'success');
        header('Content-Type: application/json');
        $json_result = json_encode($myReturn,JSON_UNESCAPED_UNICODE);
        echo trim($json_result,'');
      }else{
        $myReturn = array(
            'status'  => $resultFromDB['status'],
            'message'   => $resultFromDB['errorMsg']
          );
        header('Content-Type: application/json');
        echo json_encode($return,JSON_UNESCAPED_UNICODE);
      }

when result status from function = 0, then result this code should be:

{"status":"0","message":"success"}

when sent through Idea IntellIJ Test RESTful webservice, I get the result:


{"status":"0","message":"success"}

how to correctly return the result, that I would not have extra characters 

Saahon
  • 404
  • 1
  • 6
  • 27

1 Answers1

0

I have previously experienced this problem myself, when using Notepad Plus Plus on Windows. I was able to resolve the error by ensuring that there is no whitespace at the beginning of my file, and checking that it was UTF-8 Without BOM (Through the menu Encoding > Convert to UTF-8).

Ensure you have no characters at the start of your file, you can use a hex plugin for Notepad Plus Plus to do this, each character will be referenced by a hex character at the start, if there are any hex characters before the one referencing your first character, remove them.

You will have to do this for all your PHP files, but UTF-8 Without BOM is commonly regarded as the correct encoding for PHP.

Once you have checked all this, I expect you should no longer have this problem.

Michael Thompson
  • 541
  • 4
  • 21
  • That's not a programmable answer thou. You'd have to manually do that to each time you retrieve data from a source. – FabricioG Mar 22 '19 at 00:37
  • Yes it is, because the character isn't coming from the data source (which in this case appears to be a database), it is coming from the PHP file which is in the wrong encoding. Changing the encoding to UTF-8 Without BOM will fix the problem. If it were to come from some source because some 3rd party hasn't checked their code before they shipped it, for whatever reason, then just play around with trimming the left characters until you get the desired effect (I would expect that to be 4 characters because I think there's an invisible there too). https://stackoverflow.com/questions/4286423 – Michael Thompson Mar 22 '19 at 06:55