如何访问此结果

时间:2021-05-24 23:52:08

I have this result from my server and I want to access the value of mem and pid

我从我的服务器得到这个结果,我想访问mem和pid的值

{"mem":"7","pid":"5"}{"mem":"9","pid":"7"}{"mem":"10","pid":"7"}{"mem":"8","pid":"5"}

I tried echo the value from my console.but it did not show.

我尝试从我的控制台回显值。但它没有显示。

 $.ajax({
                type:'post',
                dataType:'json',
                data:'mydata'
                url:'tomyurl',
                success:function(data){
                 console.log(data[0].mem);//did not show the value


                   });

EDIT

here is my server side script

这是我的服务器端脚本

  public function display_children($parent,$level){
 try {
          $cmd = $this->connection->prepare('SELECT mem,pid from mytree where pid = ?');

          $cmd->execute(array($parent));


          $results = array(
              'mem' => array(),
              'pid' => array()

          );

          while ( $row =  $cmd->fetch(PDO::FETCH_ASSOC)) {


              $results['mem']=$row['mem'];
              $results['pid']=$row['pid'];

              echo json_encode(results);
              $this->display_children($row['mem'], $level + 1);

          }


      }
      catch(PDOException $ex){
          return $ex->getMessage();
      }

  }

I followed here on how to query hierarchical-data-database

我在这里关于如何查询分层数据 - 数据库

and I'm having problem in accessing the value of my mem and pid.

我在访问我的mem和pid的值时遇到了问题。

Thank you in advance.

先谢谢你。

1 个解决方案

#1


1  

That's not valid JSON as written. You probably want to ensure that your server is returning a valid array, like:

这不是写的有效JSON。您可能希望确保服务器返回有效数组,例如:

[{"mem":"7","pid":"5"},{"mem":"9","pid":"7"},{"mem":"10","pid":"7"},{"mem":"8","pid":"5"}]

If it is, the code you've supplied should work. To get all the mem and pid values in the array, you can do something like:

如果是,您提供的代码应该可以使用。要获取数组中的所有mem和pid值,您可以执行以下操作:

for (var index = 0; index < data.length; index++) {
    var item = data[index];
    console.log("index=" + index + ", pid=" + item.pid + ", mem=" + item.mem);
}

http://jsfiddle.net/cn7mzxws/1/

Since you're using jQuery, you can also use $.each to accomplish the same thing.

既然你正在使用jQuery,你也可以使用$ .each来完成同样的事情。

Though if you only care about the mem value for the first item, then data[0].mem is correct, once your data is correctly packed into a JSON-array.

虽然如果您只关心第一项的mem值,那么一旦您的数据正确打包到JSON数组中,data [0] .mem就是正确的。

#1


1  

That's not valid JSON as written. You probably want to ensure that your server is returning a valid array, like:

这不是写的有效JSON。您可能希望确保服务器返回有效数组,例如:

[{"mem":"7","pid":"5"},{"mem":"9","pid":"7"},{"mem":"10","pid":"7"},{"mem":"8","pid":"5"}]

If it is, the code you've supplied should work. To get all the mem and pid values in the array, you can do something like:

如果是,您提供的代码应该可以使用。要获取数组中的所有mem和pid值,您可以执行以下操作:

for (var index = 0; index < data.length; index++) {
    var item = data[index];
    console.log("index=" + index + ", pid=" + item.pid + ", mem=" + item.mem);
}

http://jsfiddle.net/cn7mzxws/1/

Since you're using jQuery, you can also use $.each to accomplish the same thing.

既然你正在使用jQuery,你也可以使用$ .each来完成同样的事情。

Though if you only care about the mem value for the first item, then data[0].mem is correct, once your data is correctly packed into a JSON-array.

虽然如果您只关心第一项的mem值,那么一旦您的数据正确打包到JSON数组中,data [0] .mem就是正确的。