php json轉數組出錯

來源:互聯網
上載者:User
php json轉數組出錯求助
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; $iif (!$comment){
if ( ($json[$i] == '{') || ($json[$i] == '[') )
$out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']'))
$out .= ')';
else if ($json[$i] == ':')
$out .= '=>';
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));

function _json_decode($json){
$comment = false;
$out = '$x=';
for ($i=0; $i if (!$comment){
if ( ($json[$i] == '{')
------解決方案--------------------
($json[$i] == '[') )
$out .= ' array(';
else if (($json[$i] == '}')
------解決方案--------------------
($json[$i] == ']'))
$out .= ')';
else if ($json[$i] == ':')
$out .= '=>';
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] =>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.