Ofir Malachi
Ofir Malachi

Reputation: 1286

PARSE json string to json array (from GET)

i'm tring to parse url's json string to an json array.

ISSUE: json_decode = empty

QUESTION: Does anyone see what am i doing wrong?

MY STEPS:

tests from browser:

.....send.php?{"contactName":"name1"},{"contactName":"name2"}

my php code:

1. $url = $_SERVER['QUERY_STRING'];
2. $urlStringDecoded = urldecode($url);

echo urlStringDecoded result ok:

{"contactName":"name1"},{"contactName":"name2"}

3. $json = json_decode($urlStringDecoded, true);

RESULT EMPTY

echo("$json");

Upvotes: 0

Views: 64

Answers (2)

Jenson M John
Jenson M John

Reputation: 5689

Seems like your JSON is invalid. Wrap your current string within [ ] Brackets.

Eg.

<?php
$url = $_SERVER['QUERY_STRING'];
//echo $url;
$urlStringDecoded = urldecode($url);
echo $urlStringDecoded;
$json = json_decode("[".$urlStringDecoded."]", true);
echo "<pre>";
print_r($json);
echo "</pre>";
?>

Upvotes: 2

Rahul
Rahul

Reputation: 685

As you are trying to convert json string to json, you should provide right format. From you example it looks like you are passing , comma separated json objects whereas it should be an array. Add [] around your string and then try, in other words make it an array.

Upvotes: 0

Related Questions