dongzhan8620 2016-08-30 10:57
浏览 49

如何将变量赋给json回复

I want to assign a variable to my json reply. I want to use that variable to echo only the selected data

This is my reply:

{"code":"202","message":"Accepted","data":{"resultMap":{"D1509~H24":{"AppDay":"*","HosTown":"Colombo","SpecName":"Physiotherapist","HosName":"Western Infirmary Hospital (Pvt) Ltd","SpecializationId":"34","HosCode":"H24","AppDate":"Any","DocName":"MR M. HAJITH","DoctorNo":"D1509"},"D0625~H08":{"AppDay":"*","HosTown":"Colombo","SpecName":"Physiotherapist","HosName":"Park Hospital (Pvt) Ltd ","SpecializationId":"34","HosCode":"H08","AppDate":"Any","DocName":"MR JALIYA UDUWELLA","DoctorNo":"D0625"},"D1762~H16":

This is my code but I am getting an error message:

Undefined index: resultMap in C:\wamp\www\echanalling\test.php

on

if (isset($_POST['submit']))
 {
require 'gettoken1.php';                                
            {
$baseurl = 'http://202.124.173.189/api/v1/doctorAvailability';    
$curl = curl_init($baseurl);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json',"Authorization: Bearer $atoken")); 

// Set the POST arguments to pass to the Sugar server
$rawPOSTdata = array(
    "type" => "private",
    "hosID" => $_POST['hosMaptxt'],
    "specID" => $_POST['specialityMaptxt'],
     "date" => $_POST['date'],
    "name" => '%'.$_POST['docname'].'%'
);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($rawPOSTdata));    

$response = curl_exec($curl);
curl_close($curl);
if (!$response)
    {
    die("Connection Failure.
");

$result = json_decode($response,true);    

 $json=$result['data']['resultMap'];
$html=array();
$output[]="<table><tr> ";
foreach($result['resultMap']as $data)
{
    $output.="<td>".$data['SpecName']."</td>";
    $output.="<td>".$data['HosName']."</td>";
    $output.="<td>".$data['DocName']."</td>";
    $output.="<td>".$data['DoctorNo']."</td>";
    $output.="<td>".$data['AppDay']."</td>";
    $output.="<td>".$data['HosTown']."</td>";
            }
            $output .="</tr></table>";
            echo $output;        
}

curl_close($curl);
if ( isset($result->error) )
    {
    die($result->error_message."
");
    } 
 }    
?>
  • 写回答

1条回答 默认 最新

  • duanliyi5997 2016-08-31 04:01
    关注

    Your question doesn't show sufficient complete and precise code to be sure, but at a first glance we can guess that:

    1. $result = json_decode($response,true); is where you get the contents of your JSON data
    2. foreach($result['resultMap']as $data) is where you get the error

    So from #1 above comes that what you get in $result should look like:

    [
      "code" => "202",
      "message" => "Accepted",
      "data" => [
        "resultMap" => [
          "D1509~H24" => [
            "AppDay" => "*",
            ...
          ],
          "D0625~H08" => [
            "AppDay" => "*",
            ...
          ],
          ...
        ]
      ]
    ]
    

    Then from #2 above it's perfectly normal you get an error trying to access $result['resultMap'], which is not a 1st-level key of $result.

    To access this part of the data you should write $result['data']['resultMap'] instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)