douxiong4250 2017-03-25 22:28
浏览 60
已采纳

Json在插入时解码未定义的索引

json


  {
      "data": {
        "acct": [
          {
            "maxaddons": "1",
            "ip": "192.168.0.20",
            "ipv6": ["0101:ca75:0101:ca75:0101:ca75:0101:ca77"],
            "outgoing_mail_suspended": 0,
            "outgoing_mail_hold": 0,
            "min_defer_fail_to_trigger_protection": 5,
            "legacy_backup": 0,
            "diskused": "0M",
            "maxftp": "2",
            "startdate": "13 Jul 08 14:33",
            "max_defer_fail_percentage": "10",
            "disklimit": "100M",
            "is_locked": "0",
            "suspendtime": null,
            "email": "",
            "domain": "example.com",
            "unix_startdate": 1373312011,
            "user": "example",
            "plan": "plan9",
            "shell": "\/bin\/bash",
            "maxpop": "20",
            "backup": 0,
            "theme": "paper_lantern",
            "owner": "root",
            "max_email_per_hour": "100",
            "suspendreason": "not suspended",
            "maxlst": "5",
            "suspended": 0,
            "maxsql": "1",
            "maxparked": "1",
            "partition": "home",
            "maxsub": "5"
          },
        ]
      },
      "metadata": {
        "version": 1,
        "reason": "OK",
        "result": 1,
        "command": "listaccts"
      }
    } 

//controller

public function ApiAction()
      {
          $Cpanel = new \Gufy\CpanelPhp\Cpanel;
          $Cpanel->setAuthType('hash');
          $Cpanel->setHost($this->container->getParameter('api_host'));
          $Cpanel->setAuthorization($this->container->getParameter('api_user'), $this->container->getParameter('api_hash'));
          $Cpanel->setTimeout(50);

          $Arguments = array();
          $QueryCpanel= $Cpanel->__call('listaccts', $Arguments);
          $em = $this->getDoctrine()->getEntityManager();
          $json = json_decode($QueryCpanel, true);

          foreach ($json as $List) {
            $Data['Domain'] =$List ['domain'];
            $Data['IP'] = $List ['ip'];
            $Data['UserName'] = $List ['user'];
            $Data['Email'] = $List ['email'];
            $Data['StartDate'] = $List ['startdate'];
            $Data['DiskPartition'] = $List ['partition'];
            $Data['Quota'] = $List ['disklimit'];
            $Data['DiskSpaceUsed'] = $List ['diskused'];
            $Data['Package'] = $List ['plan'];
            $Data['Theme'] = $List ['theme'];
            $Data['Owner'] = $List ['owner'];
            $Data['UnixStartDate'] = $List ['unix_startdate'];

              $em->getRepository('AppBundle:Listaccts')
                  ->InsertListaccts('listaccts', $Data);
        }

Iam trying to insert data from api to database and I keep getting error Notice: Undefined index: domain

If refresh my browser then I get this error

An exception occurred while executing 'INSERT INTO (Domain, IP, UserName, Email, StartDate, DiskPartition, Quota, DiskSpaceUsed, Package, Theme, Owner, UnixStartDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params [null, null, null, null, null, null, null, null, null, null, null, null]:

I also tried with controller persist I got same error there. I dont know what iam doing wrong.

  • 写回答

1条回答 默认 最新

  • duankuan5319 2017-03-25 22:59
    关注

    If the structure of the JSON string from the API is as shown above, then apparently the access method is somewhat incorrect.

    The correct method should be as follows:

    $json = json_decode($QueryCpanel, true);
    foreach($json["data"]["acct"] as $list){
        echo "Domain: ".$list["domain"];
    }
    

    Demo

    An assumption here is that in case of API returning multiple accounts, they will all be supplied as arrays under the data index.

    Although it might be unlikely, but if at all your array structure repeats within data directly, then the foreach and subsequent referencing would need a little bit of change. Maybe something on the lines of:

    foreach($json["data"] as $list){
        ....
    }
    

    In any case, if you use print_r(json);, you'd be able to get an idea on the access route to take to get to any index.

    Hope this helps. Do let me know if I'm misinterpreting something.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥15 sqpingboot在父模块已经引入了mybatisplus 在子组件不写版本号还是报错
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么