Json_decode returns Null and Error 4

I am getting null when I decode my Json. $ contactInfo is a string from Java (Android) pushed through Volley. I checked the string, this is the string.

I have tried the following

1) Create $ contactInfo with only one user contact

2) Removed spaces

3) Remove special characters like β™‘

4) Remove +

The closest solution I can find for reading such [[data, data], [data, data]] is using json decoding. Is there any other way for me to read this in an array or json?

$contactInfo = "[[john qiu, +16266169643], [Vince, +65 5888 8447]]"
$obj = stripslashes($contactInfo);
$obj = json_decode($obj);
echo json_last_error(); 

      

echo returns JSON_ERROR_SYNTAX

contactInfo, some users have special characters like "" "

+3


source to share


4 answers


Correct JSON format

$contactInfo = '[["john qiu", 16266169643], ["Vince", 6558888447]]"

      



You can check your result to see if it is correct on some website like this , like

+1


source


How about this code



$contactInfo = "[[john qiu, +1626643]]";
$obj = json_decode($contactInfo, true);
var_dump($obj);

      

0


source


$contactInfo = "[[john qiu, +16266169643], [Vince, +65 5888 8447]]" 

      

In PHP above, $ contactInfo is an array. To avoid any error, make sure the numeric values ​​are in quotation marks. eg:

$contactInfo = "[['john qiu', '+16266169643'], ['Vince', '+65 5888 8447']]" 

      

0


source


The whole string must be wrapped in single quotes, which allows you to encapsulate json string segments in double quotes

<?php
$contactInfo = '[["john qiu", "+16266169643"], ["Vince", "+65 5888 8447"]]';
json_decode($contactInfo, true);
echo json_last_error(); 
?>
<b>output 0</b>

      

If your data is not formatted that way you might have to do some clever preg_replace

0


source







All Articles