3

I'm getting null when I decode my Json. $contactInfo is a String from Java(Android) pushed via Volley. I did check the string, it is a string.

I have tried the following

1) Make the $contactInfo with one user's contact only

2) Removed spaces

3) Remove special characters like ♡

4) Remove +

The closest solution I can find to reading such [[data,data],[data,data]] is to use json decode. Is there any other way for me to read this into a array or json?

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

echo is returning JSON_ERROR_SYNTAX

contactInfo, for some users , there are special characters like "♡"

11
  • Where is $data, the error is pretty self-explanatory. It isn't valid JSON syntax. [[john qiu, +1626643]] isn't valid JSON if that's what you are referring to. Commented May 13, 2015 at 2:08
  • @Devon The closest solution I can find to reading such [[data,data],[data,data]] is to use json decode. Is there any other way for me to read this into a array or json? Commented May 13, 2015 at 2:14
  • Where are you getting this data from? There are certainly other ways, but it'd probably just be easier to address the format of the data at the source. Commented May 13, 2015 at 2:16
  • [[john qiu, +1626643]] is not a valid JSON {[john qiu, +1626643]} should be. Can u dump the whole string you are getting returned, and tell us if the form is always the same, maybe we can figure out a function to decode it. Commented May 13, 2015 at 2:20
  • @PawelBieszczad I tried hard coding {[john qiu, +1626643]}. I have updated the "whole dump" I received Commented May 13, 2015 at 2:57

4 Answers 4

2

The correct JSON format is

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

You can validate your output, to see if its right on some website like this one for example

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

Comments

0

How about this code

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

1 Comment

Nope :( Getting null still
0
$contactInfo = "[[john qiu, +16266169643], [Vince, +65 5888 8447]]" 

In PHP, above $contactInfo itself is an array. To avoid any error, please ensure the non-numeric values are in quotes. eg:

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

Comments

0

The whole string should be wrapped in single quotes allowing you to encapsulate the 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 doesn't come formatted like this you may have to do some clever preg_replace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.