【问题标题】:Codeigniter Rest Server JSON multidimension issueCodeigniter Rest Server JSON 多维问题
【发布时间】:2016-04-08 13:43:29
【问题描述】:

我正在使用 Codeigniter 休息服务器来创建一个 api。我的一个客户正在向我的 api 发送以下 JSON 数组

    {
     "code": "TEST",
     "store": "DBNG0024",
     "total": "50.00",
     "items": [{ "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1",    "dept":"1"}]
    }

在其余服务器文档中说您可以访问以下数据:

   function client_post()
   {
      $code = $this->post('code');
      $store_code = $this->post('store');
      $total = $this->post('total');

      $data = array('code' => $this->post('code'), 'store' => $this->post('store'), 'status' => 'invalid', 'value' => '0', 'message' => 'code is invalid');

      $this->response($data);
   } 

这非常有效。现在我遇到的问题是我无法访问多维数据“items”:[{“code”:“121”,“descr”:“Pizza 1”,“value”:“50”,“qty”: "1", "dept":"1"}]

我已经尝试了以下

    $items = json_decode($this->post(‘items’)); - Prints nothing
    $items = $this->post(‘items’); - prints the word Array

如果我打印这样的帖子数据 print_r($_POST,true);以下是打印的内容

    Array ( [code] => 1234 [store] => 1234 [total] => 1234 [items] => Array )

谁能帮助我找到访问 $items 数组数据的方法

提前谢谢你

【问题讨论】:

    标签: arrays json codeigniter multidimensional-array codeigniter-restserver


    【解决方案1】:

    正如您所说,您在打印时将项目作为数组获取,您可以通过访问其键值对来循环遍历它

    foreach($_POST['items'] as $key=>$value)
    {
      echo 'Key =>'.$key.'|---| Value =>'.$value;
    }
    

    希望对您有所帮助,如果您卡在某个地方,请告诉我。

    编辑: 试试这个,我可能会 json_decode 你的完整 POSTED 数据..试试下面的代码

    // $json will contain your posted data
    $json = '{"code": "TEST","store": "DBNG0024","total": "50.00","items": [{ "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1","dept":"1"}]}';
            $items = json_decode($json)->items;
    
            "qty"=>"1", "dept"=>"1"];
            foreach($items[0] as $key=>$value)
            {
                echo 'Key =>'.$key.':: Value =>'.$value;
                echo '<br/>';
            }
    

    【讨论】:

    • 嗨,我确实试过这个,但我得到以下错误 - 为 foreach() 提供的参数无效
    • 我如何获取帖子数据,它是由另一家正在访问我们的 API Url 的公司发送给我们的
    • 如果他们发帖,你可以使用 $this->post('json')
    • 我没有这部分 - $json = '{"code": "TEST","store": "DBNG0024","total": "50.00","items": [ { "code":"121", "descr":"Pizza 1", "value":"50", "qty":"1","dept":"1"}]}';它以 JSON 数组格式发送到 API url
    • 正如你所说,你有 $_POST 正在打印数据然后你可以直接 json_decode($_POST) 它应该可以工作
    【解决方案2】:

    通常,解码 JSON 后,它会默默地成为stdClass 的兄弟。 如果您能够以$_POST 的形式检索值codestore_codetotal,那么您也必须能够检索items

    $code  = $this->post('code');
    $items = json_decode($this->post('items'));
    
    foreach($items as $item)
       echo $item->value;
    

    【讨论】:

      【解决方案3】:

      我认为$items = $this-&gt;post(‘items’); 是访问“项目”的正确方法,请查看此链接Send a raw json value to rest controller。可能你打印数组的方式不对,也请查看这个链接The word “Array” gets printed instead of the values of the rows

      【讨论】:

      • 感谢您的评论,将查看链接。
      猜你喜欢
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      相关资源
      最近更新 更多