如何使用php修复JSON格式
我试图将json格式发送到我的android应用程序,但我发现我的json格式不正确如何使用php修复JSON格式,php,Php,我试图将json格式发送到我的android应用程序,但我发现我的json格式不正确 {data={"image":"http:\/\/www.androidhive.info\/wp-content\/uploads\/2016\/01\/Air-1.png","message":{"chat_room_id":"","created_at":"2017-03-22 3:34:30","message_id":"","message":"77"},"user":{"user_id":null,
{data={"image":"http:\/\/www.androidhive.info\/wp-content\/uploads\/2016\/01\/Air-1.png","message":{"chat_room_id":"","created_at":"2017-03-22 3:34:30","message_id":"","message":"77"},"user":{"user_id":null,"gcm_registration_id":null,"name":null,"created_at":null,"email":null}}, flag=0, title=Google Cloud Messaging, is_background=false}
我这样设置数据:
$app->post('/users/send_to_all'
function() use ($app) {
$response = array();
verifyRequiredParams(array('user_id', 'message'));
require_once __DIR__ . '/../libs/gcm/gcm.php';
require_once __DIR__ . '/../libs/gcm/push.php';
$db = new DbHandler();
$user_id = $app->request->post('user_id');
$message = $app->request->post('message');
require_once __DIR__ . '/../libs/gcm/gcm.php';
require_once __DIR__ . '/../libs/gcm/push.php';
$gcm = new GCM();
$push = new Push();
//get the user using userid
$user = $db->getUser($user_id);
//creating tmp message , skipping database insertion
$msg = array();
$msg['message'] = $message;
$msg['message_id'] = '';
$msg['chat_room_id'] = '';
$msg['created_at'] = date('Y-m-d G:i:s');
$data = array();
$data['user'] = $user;
$data['message'] = $msg;
$data['image'] = 'http://www.androidhive.info/wp-content/uploads/2016/01/Air-1.png';
$push->setTitle("Google Cloud Messaging");
$push->setIsBackground(FALSE);
$push->setFlag(PUSH_FLAG_USER);
$push->setData($data);
//sending message to topic `global`
//on the device every user should subscribe to `global` topic
$gcm->sendToTopic('global', $push->getPush());
$response['user'] = $user;
$response['error'] = false;
echoRespnse(200, $response);
});
下面是我的Push.php
关于getPush()
:
这是我的Gcm.php
关于sendToTopic
:
//sending message to a topic by topic id
public function sendToTopic($to, $message) {
$fields = array(
'to' => '/topics/' . $to,
'data' => $message,
);
return $this->sendPushNotification($fields);
}
我真的不熟悉php,如何修复JSON格式
任何帮助都将不胜感激,提前感谢
我更新了我的问题,在我的Gcm.php中找到了json_encode:
它位于curl\u setopt($ch,CURLOPT\u POSTFIELDS,json\u encode($fields));
仍然让我的json格式不正确,如何修复?您的json无效。请尝试:
{"data":{"image":"http:\/\/www.androidhive.info\/wp-content\/uploads\/2016\/01\/Air-1.png","message":{"chat_room_id":"","created_at":"2017-03-22 3:34:30","message_id":"","message":"77"},"user":{"user_id":null,"gcm_registration_id":null,"name":null,"created_at":null,"email":null}}, "flag":0, "title":"Google Cloud Messaging", "is_background":false}
注意,我用:
替换了=
的用法,并将键和字符串用引号括起来(“”
要使用php将其转换成一个漂亮的json字符串,首先需要正确构建数组。可能的方法是设置一个响应数组,然后可以json\u encode()
$response =[
'data' => $data,
'flag' => 0,
'title' => 'Google Cloud Messaging',
'is_background' => false.
];
现在你可以
json_encode($response);
您的JSON无效。请尝试:
{"data":{"image":"http:\/\/www.androidhive.info\/wp-content\/uploads\/2016\/01\/Air-1.png","message":{"chat_room_id":"","created_at":"2017-03-22 3:34:30","message_id":"","message":"77"},"user":{"user_id":null,"gcm_registration_id":null,"name":null,"created_at":null,"email":null}}, "flag":0, "title":"Google Cloud Messaging", "is_background":false}
注意,我用:
替换了=
的用法,并将键和字符串用引号括起来(“”
要使用php将其转换成一个漂亮的json字符串,首先需要正确构建数组。可能的方法是设置一个响应数组,然后可以json\u encode()
$response =[
'data' => $data,
'flag' => 0,
'title' => 'Google Cloud Messaging',
'is_background' => false.
];
现在你可以
json_encode($response);
PHP有一些内置的json函数,您需要json_encode()和json_decode() 例如,此代码:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
下面是json_encode()和整个过程的示例
希望这有帮助。PHP有一些内置的json函数,您需要json_encode()和json_decode() 例如,此代码:
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
下面是json_encode()和整个过程的示例
希望这有帮助。只需使用json_encode($response);我应该在哪里使用此代码?函数
getPush()是否关于返回json_encode($res)
?我已经尝试了return json_encode($res);
ongetPush()
,这会导致我无法将json信息发送到我的应用程序。只需使用json_encode($response);我应该在哪里使用此代码?函数getPush()是否关于返回json_encode($res)
?我已经尝试了返回json_encode($res);
关于getPush()
,这导致我无法将json信息发送到我的应用程序。谢谢你的回答,但我不知道如何在我的情况下通过php发送正确的json格式。你只需要首先正确构建php数组。请查看我在回答中的更新是否有帮助。谢谢,但是我尝试了你的代码,而不是getPush()
,无法将消息发送到我的应用程序。谢谢你的回答,但我不知道在我的情况下如何通过php发送正确的json格式。你只需要首先正确构建php数组。请查看我的回答中的更新是否有帮助。谢谢,但是我尝试了你的代码,而不是getPush()
,无法将消息发送到我的应用程序