0

I have some problem with JSON. How can I make my array "$notify_id" a JSON array? Cause when I run my push web application it will answer me "Field 'data' must be a JSON array: {"registration_id":["14096543677114293"]} ".

Please help

   function insertNotificationStartTime($registration_id) {
        $timestamp = time();
        $start_notify = date("Y-m-d H:i:s",$timestamp);
        for ($i = 0; $i < count($registration_id); $i++) {
            $random = rand(1, 10000000);
            $query = "INSERT INTO notify (id,registration_id,start_notify) VALUES ('" .$timestamp.$random. "','" .$registration_id[$i]. "','" .$start_notify. "')";
            $result = mysql_query($query);
            if (!$result) {
                die('Invalid query: ' . mysql_error());
            }
            $notify_id[] = $timestamp.$random;
        }
        return $notify_id;
    }
3
  • 3
    return json_encode($notify_id); Commented Sep 2, 2014 at 10:56
  • Read up on json_encode Commented Sep 2, 2014 at 11:02
  • As per your example JSON at the top, you'll need to echo json_encode(array('registration_id' => $notify_id)); Commented Sep 2, 2014 at 11:05

5 Answers 5

1

you have to send Json header

header('Content-Type: application/json');
echo json_encode($notify_id);
Sign up to request clarification or add additional context in comments.

Comments

1

simply add

echo json_encode($notify_id);

at end of your code this function print array of your data..in your browser u must have json viewer extension so u can view your json data as array..

use this link

[1]: http://jsonlint.com/

put your code page link in that box check whether your json program is right or wrong

Comments

0

Use

json_encode($notify_id);

If you get an error, Verify that the output is valid json at http://jsonlint.com.

Comments

0

According to your code the function is returning an array, and you have to pass to json_encode the result of this function.

$result = insertNotificationStartTime($registration_id);
header('Content-Type: application/json');
echo json_encode($result);

Please have a look on json_encode function

Comments

0

Script for backward-compatibility: https://github.com/douglascrockford/JSON-js/blob/master/json2.js

And call:

var myJsonString = JSON.stringify(yourArray);

Note: The JSON object is now part of most modern web browsers (IE 8 & above). See caniuse for full listing.

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.