Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.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没有';t解析我的json输出_Php_Json - Fatal编程技术网

PHP没有';t解析我的json输出

PHP没有';t解析我的json输出,php,json,Php,Json,您好,我想在php中读取json数据。我创建了我的服务,但是这里有一个问题,我期望的字符串没有显示在我的网页中。然后我创建了一个测试服务,比之前的服务简单了一点。没有什么变化 预期结果为:“会话不为空” 结果是:“{” 我的服务: $service = $_GET['service']; switch($service) { case "create_ktt": saveInfo(); break; case "create_user": createUser(); break;

您好,我想在php中读取json数据。我创建了我的服务,但是这里有一个问题,我期望的字符串没有显示在我的网页中。然后我创建了一个测试服务,比之前的服务简单了一点。没有什么变化

预期结果为:“会话不为空”

结果是:“{”

我的服务:

    $service = $_GET['service'];

switch($service)
{
case "create_ktt":  saveInfo(); break;
case "create_user": createUser();   break;
case "login_user":  loginUser();    break;
case "logout_user": logoffUser();   break;
case "get_session": getSession();   break;
case "get_report":  getReport();    break;
case "test_json":
    header('Content-type: application/json');
    echo json_encode('{"session_code":"1","message":"Session is Not Null"}');
    break;
default:
    sendError(2);
    break;
}
我的阅读区:

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'http://e-trafik.esy.es/external_services.php?service=test_json');
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 0);
$result_json = curl_exec($curl);

$decodeislemi = json_decode($result_json);

$mesaj = $decodeislemi[message];

echo $mesaj;


PS:我将我的$mesaj=$decodeislemi[message];行更改为$mesaj=$decodeislemi->message;没有任何更改。

在JSON验证程序中测试服务的URL表明您的JSON无效

问题是您正在对JSON进行双重编码。正如建议的那样,您应该向
JSON\u encode()
传递一个key:value数组,或者返回字符串(不使用
JSON\u encode
),但不能同时返回这两个字符串

这里有一个比较:

您的服务当前正在返回以下内容:

"{\"session_code\":\"1\",\"message\":\"Session is Not Null\"}"
{"session_code":"1","message":"Session is Not Null"}
您的服务应返回以下内容:

"{\"session_code\":\"1\",\"message\":\"Session is Not Null\"}"
{"session_code":"1","message":"Session is Not Null"}
要解决此问题,您需要替换此:

echo json_encode('{"session_code":"1","message":"Session is Not Null"}');
用这两个中的任何一个

echo json_encode(["session_code"=>"1","message"=>"Session is Not Null"]);
echo '{"session_code":"1","message":"Session is Not Null"}';

看起来您正在尝试构造自己的JSON字符串,然后对其进行编码?请尝试使用数组:

echo json_encode(["session_code"=>"1","message"=>"Session is Not Null"]);
或者我想您可以自己构建(不推荐)并响应它:

echo '{"session_code":"1","message":"Session is Not Null"}';

您正在对已经包含json对象的字符串进行json编码。(此外,您还未启用
error\u reporting