如何将变量赋给json回复

时间:2016-08-30 10:57:22

标签: php json

我想为我的json回复分配一个变量。我想使用该变量仅回显所选数据

这是我的回复:

IsCovariantIEnumerable(...)

这是我的代码,但收到错误消息:

  

未定义的索引:C:\ wamp \ www \ echanalling \ test.php中的resultMap

on

{"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":

1 个答案:

答案 0 :(得分:0)

您的问题并未显示足够完整且准确的代码,但乍一看我们可以猜到:

  1. $result = json_decode($response,true);是获取JSON数据内容的地方
  2. foreach($result['resultMap']as $data)是您收到错误的地方
  3. 从上面的#1开始,你在$result中得到的内容应该是这样的:

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

    然后从上面的#2开始,您尝试访问$result['resultMap']时出现错误是完全正常的,$result不是$result['data']['resultMap']的第一级密钥。

    要访问这部分数据,您应该改为 <input type="button" id="btnNew" class="btnNew" value="New" /> //javascript $("#btnNew").click(function() { console.log("show"); $("#diva").style.display = "block"; });