2

I am trying to parse a JSONArray but unable to do so. I am getting Null as reply

MY JSONArray:

["{\"mapurl\":\"http:\\\/\\\/maps.google.com\\\/maps?q=17.XXXXXXXX,78.xxxxxxx\",\"caller\":\"+91xxxxxxxxx\",\"id\":1,\"reciever\":\"+91xxxxxxxx\",\"timpestamp\":\"3\"}","{\"mapurl\":\"http:\\\/\\\/maps.google.com\\\/maps?q=17.xxxxxxx,78.xxxxxxx\",\"caller\":\"+91xxxxxxxxxx\",\"id\":2,\"reciever\":\"+91xxxxxxxxxx\",\"timpestamp\":\"3\"}"]

I am passing this JSONArray from Android as a GET request in the parameter "add"

my PHP CODE:

<?php
if (isset($_GET['add'])) {
$jsonData = $_GET['add'];
$phpArray = json_decode($jsonData);
var_dump($phpArray);
}
else{
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
asked Nov 26, 2014 at 11:54
8
  • 1
    what happens when you perform echo $_GET['add']; at the top of your code? Commented Nov 26, 2014 at 11:55
  • @RichardBernards let me check Commented Nov 26, 2014 at 11:57
  • 1
    I strongly recommand to use POST instead of GET. Commented Nov 26, 2014 at 11:59
  • If you get NULL, your JSON isn't properly encoded. Use if ($phpArray == null) echo json_last_error_msg(); to see the error. Commented Nov 26, 2014 at 11:59
  • @RichardBernards [\"{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxx,78.xxxxxx\\\",\\\"caller\\\":\\\"+91XXXXXXX\\\",\\\"id\\\":1,\\\"reciever\\\":\\\"+91XXXXXXX\\\",\\\"timpestamp\\\":\\\"3\\\"}\",\"{\\\"mapurl\\\":\\\"http:\\\\\\/\\\\\\/maps.google.com\\\\\\/maps?q=17.xxxxxx,78.xxxxxx\\\",\\\"caller\\\":\\\"+91xxxxxxxx\\\",\\\"id\\\":2,\\\"reciever\\\":\\\"+91xxxxxxxx\\\",\\\"timpestamp\\\":\\\"3\\\"}\"]NULL this is the response that i am getting Commented Nov 26, 2014 at 12:06

3 Answers 3

1

Looks as if your PHP configuration still has the deprecated magic_quotes feature enabled. Either disable magic_quotes in your PHP configuration or try to remove the extra backslashes added by the magic_quotes feature with the stripslashes() function:

$jsonData = stripslashes($_GET['add']);

answered Nov 26, 2014 at 12:17
Sign up to request clarification or add additional context in comments.

Comments

0

I experimented with your json. It's actually valid. I think it's because you're using $_GET to pass the data. I suggest you use post to send the Json data. However, if you must use $_GET to send the data, make sure you JSON.stringify() it. But if your data will be longer than what you have here, you might have a truncated url which of course, is the data.

That way your current code is very valid, including your json.

answered Nov 26, 2014 at 12:18

Comments

0

What you have here is actually several json-encoded objects stored as strings in a json array. Something funky going on with your encoder...

You can just loop through the array and decode each part:

$phpArray = array();
foreach(json_decode($_GET['add']) as $part) {
 $phpArray[] = json_decode($part, true);
}

But you should fix it at the source really!

Unsure why you're getting NULL though. You should at least get an array of strings back using your code. I tested myself and did...

answered Nov 26, 2014 at 12:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.