Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中解码多个JSON对象_Php_Json - Fatal编程技术网

在PHP中解码多个JSON对象

在PHP中解码多个JSON对象,php,json,Php,Json,我使用PHP套接字管理聊天应用程序中的数据,下面是一个示例JSON字符串,我希望从套接字读取: { "m_time" : "2015-04-07 11:37:35", "id" : "29", "msg" : "Hai there. This is a test message"} 但有时在套接字中读取多个连接的对象,如下所示: { "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"}{ "m_time" :

我使用PHP套接字管理聊天应用程序中的数据,下面是一个示例JSON字符串,我希望从套接字读取:

{ "m_time" : "2015-04-07 11:37:35", "id" : "29", "msg" : "Hai there. This is a test message"}
但有时在套接字中读取多个连接的对象,如下所示:

{ "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"}{ "m_time" : "2015-04-07 11:37:36", "id" : "31", "msg" : "Hai there 2"}{ "m_time" : "2015-04-07 11:37:37", "id" : "32", "msg" : "Hai there 3"}
无论是单个对象还是多个对象,我如何解码

用于套接字读取的PHP代码:

while(@socket_recv($changed_socket, $buf, READ_SIZE, 0) >= 1)
{
    if(!$buf) logResponse('Socket Read Failed for '. $changed_socket);

    $received_text = $buf; //unmask data
    $tst_msg = json_decode($received_text); //json decode 

    logResponse('Received Data: '. $received_text);
}
// logResponse() is used to write a log file log.html

试试这个。数组中不存在对象之间的逗号

<script type="text/javascript">
var json = [
{ "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"},
{ "m_time" : "2015-04-07 11:37:36", "id" : "31", "msg" : "Hai there 2"},
{ "m_time" : "2015-04-07 11:37:37", "id" : "32", "msg" : "Hai there 3"}
];

$.each(json, function(index, item) {
console.log(item); // this console each string {  } in json array
});
</script>

var json=[
{“m_time”:“2015-04-07 11:37:35”,“id”:“30”,“msg”:“Hai there 1”},
{“m_time”:“2015-04-07 11:37:36”,“id”:“31”,“msg”:“Hai there 2”},
{“m_time”:“2015-04-07 11:37:37”,“id”:“32”,“msg”:“Hai there 3”}
];
$.each(json,函数(索引,项){
console.log(item);//此控制台包含json数组中的每个字符串{}
});
下面是php代码示例

<?php
$arr = array(
array(
"m_time" => "2015-04-07 11:37:35",
"id" => "30",
"msg" => "Hai there 1"
),
array(
"m_time" => "2015-04-07 11:37:35",
"id" => "30",
"msg" => "Hai there 1"
)
);

echo json_encode($arr);
?>

整理输入,在每个json字符串后添加逗号,发送方式如下:

{ "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"},{ "m_time" : "2015-04-07 11:37:36", "id" : "31", "msg" : "Hai there 2"},{ "m_time" : "2015-04-07 11:37:37", "id" : "32", "msg" : "Hai there 3"},
php函数

function json_decode_multi($s, $assoc = false, $depth = 512, $options = 0) {
    if(substr($s, -1) == ',')
        $s = substr($s, 0, -1);
    return json_decode("[$s]", $assoc, $depth, $options);
}

var_dump(json_decode_multi('{ "m_time" : "2015-04-07 11:37:35", "id" : "30", "msg" : "Hai there 1"},{ "m_time" : "2015-04-07 11:37:36", "id" : "31", "msg" : "Hai there 2"},{ "m_time" : "2015-04-07 11:37:37", "id" : "32", "msg" : "Hai there 3"},'));
输出:

array(3) {
  [0] =>
  class stdClass#1 (3) {
    public $m_time =>
    string(19) "2015-04-07 11:37:35"
    public $id =>
    string(2) "30"
    public $msg =>
    string(11) "Hai there 1"
  }
  [1] =>
  class stdClass#2 (3) {
    public $m_time =>
    string(19) "2015-04-07 11:37:36"
    public $id =>
    string(2) "31"
    public $msg =>
    string(11) "Hai there 2"
  }
  [2] =>
  class stdClass#3 (3) {
    public $m_time =>
    string(19) "2015-04-07 11:37:37"
    public $id =>
    string(2) "32"
    public $msg =>
    string(11) "Hai there 3"
  }
}

您能给我们看一下您的套接字连接代码吗?对于多个对象,套接字没有返回有效的JSON字符串。如果您想使用json_decode,这个问题应该通过更改套接字代码(首选)或者通过处理您得到的输出来解决:您需要}{和[]之间的逗号在结果的开头和结尾。你可以在这里检查JSON的有效性:作者从未提到Javascript,他正在使用PHP代码。返回的结果必须有逗号。在他的示例中,逗号不存在。是的,我使用的是PHP。从套接字接收的字符串没有任何逗号分隔。从客户端看,单个jsons是发送的,但有时作为组合接收jsons@PlamenNikolov:客户端返回的结果是单一json,当我同时发送2个或更多json时,有时在php套接字中将其作为组合json读取。.可能的QDF-->$ASABLE=preg\u replace(“/\\\\\\\\\\\\{/”,“},{,$instring);它应该可以工作。但我认为您的协议设计不稳定。
@socket\u recv($changed\u socket,$buf,READ\u SIZE,0)
如果满足max
READ\u SIZE
,则可以读取半个字符串(断开)。如果数据解码失败,您应该保留最后收到的数据,并读取更多数据以附加到其中,然后重试。