• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php json转数组出错求助

php 搞代码 4年前 (2022-01-23) 15次浏览 已收录 0个评论
文章目录[隐藏]

1:先输出json

//组合医生信息JSON$jsonDoctorInfo  = "{\"mecdoctor\":[";$jsonDoctorInfo .= "{\"doctorId\":\"".$doctorId."\",\"doctorName\":\"".$strDoctorName."\", \"doctorprice\":\"".$strPrice."\",\"expertise\":\"".$strExpertise."\",\"departments\":\"".$strdepartments."\",\"doctorTitle\":\"".$strTitle."\",\"doctorPhoto\":\"".$strDoctorPhoto."\"}";$jsonDoctorInfo .= "]}";

2:获取并decode
$jsonGetDoctorInfoData = json_encode($jsonGetDoctorInfoData);
$objGetDoctorInfoData = json_decode($jsonGetDoctorInfoData);
输出:

{"mecdoctor":[{"doctorId":"","doctorName":"XXXX", "doctorprice":"122","expertise":"XXXXX","departments":"医内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}

3:将json转换成数组

//解析JSON数据	function _json_decode($json){		$comment = false;		$out = '$x=';		for ($i=0; $i';				else					$out .= $json[$i];          			}else				$out .= $json[$i];			if($json[$i]== '"' && $json[($i-1)]!="\\")				$comment = !$comment;		}		eval($out.";");				return $x;	}

4:出现问题
Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/u945504094/public_html/config/config.inc.php(876) : eval()’d code on line 1

回复讨论(解决方案)

请帮我看看到底是错到哪了。

$s = '{"mecdoctor":[{"doctorId":"","doctorName":"XXXX", "doctorprice":"122","expertise":"XXXXX","departments":"医内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(json_decode($s, true));
Array(    [mecdoctor] => Array        (            [0] => Array                (                    [doctorId] =>                     [doctorName] => XXXX                    [doctorprice] => 122                    [expertise] => XXXXX                    [departments] => 医内科                    [doctorTitle] => 主任医师                    [doctorPhoto] => hmh                )        ))

你并没有给出出错行附近的代码,自然也就不知道你遇到了什么问题了

谢谢版主,
我在第二步获取JSON后,将$s带入function _json_decode($json){中出现错误
4:出现问题
Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/u945504094/public_html/config/config.inc.php(876) : eval()’d code on line 1

$s = '{"mecdoctor":[{"doctorId":"","doctorName":"XXXX", "doctorprice":"122","expertise":"XXXXX","departments":"医内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(json_decode($s, true));
Array(    [mecdoctor] => Array        (            [0] => Array                (                    [doctorId] =>                     [doctorName] => XXXX                    [doctorprice] => 122                    [expertise] => XXXXX                    [departments] => 医内科                    [doctorTitle] => 主任医师                    [doctorPhoto] => hmh                )        ))

你并没有给出出错行附近的代码,自然也就不知道你遇到了什么问题了

另外请问一下,为什么我通过file_get_content获取的JSON字串,decode的时候加了true参数就没法解析成数组,但是直接将获取的json付给一个变量后测试就能转成数组呢

$urlGetDoctorInfo = "http://127.0.0.1:8080/mobileInterface/mobileDataInterface.php?Action=showDoctorInfo&doctorid=".$_REQUEST['doctorid'];$jsonGetDoctorInfoData =  file_get_contents($urlGetDoctorInfo);print_r(json_decode($jsonGetDoctorInfoData ,true));//数据就是空的//将字串穿付给变量后$s= '{"mecdoctor":[{"doctorId":"","doctorName":"XXX", "doctorprice":"122","expertise":"XXX","departments":"内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(json_decode($s,true));//正常输出

请问这是为什么?

没有问题呀

$s = '{"mecdoctor":[{"doctorId":"","doctorName":"XXXX", "doctorprice":"122","expertise":"XXXXX","departments":"医内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(_json_decode($s));    function _json_decode($json){        $comment = false;        $out = '$x=';        for ($i=0; $i';                else                    $out .= $json[$i];                      }else                $out .= $json[$i];            if($json[$i]== '"' && $json[($i-1)]!="\\")                $comment = !$comment;        }        eval($out.";");                 return $x<b>%本文@来源gao@!dai!ma.com搞$$代^@码!网</b><strong>搞代gaodaima码</strong>;    }
Array(    [mecdoctor] => Array        (            [0] => Array                (                    [doctorId] =>                     [doctorName] => XXXX                    [doctorprice] => 122                    [expertise] => XXXXX                    [departments] => 医内科                    [doctorTitle] => 主任医师                    [doctorPhoto] => hmh                )        ))

是啊版主,但是用file获取的就不行。
另外请问一下,为什么我通过file_get_content获取的JSON字串,decode的时候加了true参数就没法解析成数组,但是直接将获取的json付给一个变量后测试就能转成数组呢

$urlGetDoctorInfo = "http://127.0.0.1:8080/mobileInterface/mobileDataInterface.php?Action=showDoctorInfo&doctorid=".$_REQUEST['doctorid'];$jsonGetDoctorInfoData =  file_get_contents($urlGetDoctorInfo);print_r(json_decode($jsonGetDoctorInfoData ,true));//数据就是空的//将字串穿付给变量后$s= '{"mecdoctor":[{"doctorId":"","doctorName":"XXX", "doctorprice":"122","expertise":"XXX","departments":"内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(json_decode($s,true));//正常输出

请问这是为什么?

$urlGetDoctorInfo = "http://127.0.0.1:8080/mobileInterface/mobileDataInterface.php?Action=showDoctorInfo&doctorid=".$_REQUEST['doctorid'];$jsonGetDoctorInfoData =  file_get_contents($urlGetDoctorInfo);print_r(json_decode($jsonGetDoctorInfoData ,true));//数据就是空的//将字串穿付给变量后$s= '{"mecdoctor":[{"doctorId":"","doctorName":"XXX", "doctorprice":"122","expertise":"XXX","departments":"内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(json_decode($s,true));//正常输出版主大人,麻烦你看看这是什么问题呢,刚那个问题好了。

没有问题呀

$s = '{"mecdoctor":[{"doctorId":"","doctorName":"XXXX", "doctorprice":"122","expertise":"XXXXX","departments":"医内科","doctorTitle":"主任医师","doctorPhoto":"hmh"}]}';print_r(_json_decode($s));    function _json_decode($json){        $comment = false;        $out = '$x=';        for ($i=0; $i';                else                    $out .= $json[$i];                      }else                $out .= $json[$i];            if($json[$i]== '"' && $json[($i-1)]!="\\")                $comment = !$comment;        }        eval($out.";");                 return $x;    }
Array(    [mecdoctor] => Array        (            [0] => Array                (                    [doctorId] =>                     [doctorName] => XXXX                    [doctorprice] => 122                    [expertise] => XXXXX                    [departments] => 医内科                    [doctorTitle] => 主任医师                    [doctorPhoto] => hmh                )        ))

那是编码问题!
json_decode 只能识别 utf-8 编码的数据


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php json转数组出错求助

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址