2

I cannot make json_decode() to work when the string value contains single quote (') as example below:

$result = "{\"message\":\"test \' \",\"report\":[{\"1\":[{\"port\":\"gsm-1.2\",\"phonenumber\":\"XXXXXXXXXXX\",\"time\":\"2016-08-31 00:22:57\",\"result\":\"success\"}]}]}";
$resp = json_decode($result, true);
echo $resp;
5
  • stackoverflow.com/questions/8832528/… Commented Aug 30, 2016 at 17:03
  • 2
    Why are you escaping it? Just use '. Also, you can't echo the array. Commented Aug 30, 2016 at 17:04
  • 1
    And don't build JSON strings by hand if you don't have to. Build the correct object/array and then let json_encode() handle the rest. Commented Aug 30, 2016 at 17:05
  • @AbraCadaver you are right. the escaping of the single quote causes the problem. Commented Aug 30, 2016 at 17:18
  • @Sammitch I don't realy build it by hand. the result came from a result from our gsm gateway api. but that specific string values causes the error of json_decode. but abracadaver pointed out it's the escaping of single quote. Commented Aug 30, 2016 at 17:21

2 Answers 2

3

Your $result json is not in proper format so i think you need to use stripslashes() to format it and after use json_decode(). it would work :).

<?php
$result = "{\"message\":\"test \'\",\"report\":[{\"1\":[{\"port\":\"gsm-1.2\",\"phonenumber\":\"XXXXXXXXXXX\",\"time\":\"2016-08-  31 00:22:57\",\"result\":\"success\"}]}]}";
$result=stripslashes($result);
$resp = json_decode($result, true);
var_dump($resp);
?>

check on phpfiddle => http://phpfiddle.org/main/code/4e7n-vjxa

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

Comments

1

In your code the single quote(') is skipped with slash () thus it is breaking the JSON format.

Try removing the slash and try. It should work.

You should check the code where you are generating this JSON.

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.