簡體   English   中英

使用已知密鑰獲取返回的json的值

[英]Getting the value of returned json using a known key

我有這個PHP代碼返回一些json

public function my_account(){
             header("Access-Control-Allow-Origin: *");
           header("Access-Control-Allow-Methods: PUT, GET, POST");
           header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
           $email_posted = $this->input->post('email');
           $email = preg_replace('/\s+/', '', $email_posted);

            $where = "email='$email'";

            $this->db->where($where);


            $query = $this->db->get('users');

            if ($query->num_rows() > 0)
            {
               foreach ($query->result() as $row)
               {
                  $json = array("id"=>$row->id, "email"=>$row->email,"names"=>$row->names,"country"=>$row->country,"password"=>$row->password,"telephone"=>$row->telephone);
                  echo json_encode($json);
               }
            }

         }

返回的json是這種格式

{"id":"15","email":"corn64@gmail.com","names":"Cern 64","country":"","password":"cern!768","telephone":"00000"}

在jquery中獲取數據

$.each(data, function(i, obj) {
        alert(obj.email);
});

並有這個小提琴https://jsfiddle.net/c5b4o6yh/2/

我收到了錯誤

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in

{“id”:“15”,“email”:“corn64@gmail.com”,“姓名”:“Cern 64”,“國家”:“”,“密碼”:“cern!768”,“電話” : “00000”}

使用已知密鑰獲取值的正確方法是什么?

這肯定是一個問題: -

你在foreach()里面寫了你的$json變量

你在foreach()里面只發送一個單獨的回復。

更改如下代碼(更改已注釋): -

public function my_account(){
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: PUT, GET, POST");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
    $email_posted = $this->input->post('email');
    $email = preg_replace('/\s+/', '', $email_posted);

    $where = "email='$email'";

    $this->db->where($where);


    $query = $this->db->get('users');
    $json = array(); //define variable as an array variable
    if ($query->num_rows() > 0)
    {
       foreach ($query->result() as $row)
       {
          $json []= array("id"=>$row->id, "email"=>$row->email,"names"=>$row->names,"country"=>$row->country,"password"=>$row->password,"telephone"=>$row->telephone);//assign values to array

       }
    }
    echo json_encode($json); // send response after loop completion
 }

現在您的原始腳本代碼將起作用。

首先,將值保存在數組中。((使用array_push()函數可以將一個或多個元素插入到數組的末尾))

public function my_account(){
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: PUT, GET, POST");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
    $email_posted = $this->input->post('email');
    $email = preg_replace('/\s+/', '', $email_posted);

    $where = "email='$email'";

    $this->db->where($where);


    $query = $this->db->get('users');
    $json = array();
    if ($query->num_rows() > 0)
    {
       foreach ($query->result() as $row)
       {
        array_push($json,array("id"=>$row->id, "email"=>$row->email,"names"=>$row->names,"country"=>$row->country,"password"=>$row->password,"telephone"=>$row->telephone));

          echo json_encode($json);
       }
    }

}

在這種情況下,您的響應應該是數組,而不是簡單的對象。 因此,嘗試重新編寫代碼以返回

[{"id":"15","email":"corn64@gmail.com","names":"Cern 64","country":"","password":"cern!768","telephone":"00000"}]

這個jsfiddle是正確的。

使用JSON.parse(data)將JSON字符串從端點轉換為JS對象,然后獲取各個鍵值:

var obj = JSON.parse(data);
var id = obj.id;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM