【问题标题】:JSON: Display JSON result with different formatJSON:以不同格式显示 JSON 结果
【发布时间】:2020-02-14 02:16:21
【问题描述】:

我只是 JSON 的新手。根据我的问题,以下是我当前的代码广告结果

代码 1

<?php 

    require_once '../config/configPDO.php';

    header('Content-Type: application/json');

    $response = array();

    $badgeid = '10010080';
    $pwd = '10010080';

    $stmt = $conn->prepare("SELECT * FROM ot_users WHERE badgeid = '$badgeid' AND pwd = '$pwd' AND roles_id = 7 AND team_id <> 1");
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!empty($result)) {

        $response['error'] = false; 
        $response['message'] = 'Login successfull'; 
        $response['user'] = $result;  

    }else{
        $response['error'] = false; 
        $response['message'] = 'Invalid username or password';
    }


    echo json_encode($response);

?>

结果 JSON 1

{"error":false,"message":"登录成功","user":{"badgeid":"10010080","email":null,"pwd":"10010080","fullname": "AZWAN BIN SANIMIN","roles_id":"7","team_id":"2","users_id":null}}

代码 2

<?php 

    header('Content-Type: application/json');

    $response = array();

    $badgeid = '10010080';
    $pwd = '10010080';

    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd";
    $data = file_get_contents($url);
    $json = json_decode($data);
    $result = $json->otUserList;

        if (!empty($result)) {

            $response['error'] = false; 
            $response['message'] = 'Login successfull'; 
            $response['user'] = $result;  

        }else{
            $response['error'] = false; 
            $response['message'] = 'Invalid username or password';
        }

        echo json_encode($response);

?>

结果 JSON 2

{"error":false,"message":"登录成功","user":[{"badgeid":"10010080","email":"","pwd":"","fullname" :"AZWAN BIN SANIMIN","roles_id":"7","team_id":"2","users_id":""}]}

其中一个区别是“[”,结果 JSON 2 有它,而结果 1 没有。我在这里想要的是结果 2 与结果 1 相同。

谁能知道我需要在代码 2 的哪里更改代码?

谢谢。

【问题讨论】:

  • 第一个 Json 返回完全对象数据可用于每次更改
  • 你能编辑答案吗?
  • 第二个是多维的,把结果改成只得到一个列表放在user里面
  • @Kevin 你能编辑我的答案吗?请...

标签: php mysql json


【解决方案1】:

只是 php 变量将 $result 更改为 $result[0] 试试这个。无法执行您的“http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd”链接,因为密码不正确

    header('Content-Type: application/json');

    $response = array();

    $badgeid = '10010080';
    $pwd = '10010080';

    $url = "http://172.20.0.45/TGWebService/TGWebService.asmx/ot_displayUser?badgeid=$badgeid&pwd=$pwd";
    $data = file_get_contents($url);
    $json = json_decode($data);
    $result = $json->otUserList;

        if (!empty($result)) {

            $response['error'] = false; 
            $response['message'] = 'Login successfull'; 
            $response['user'] = $result[0];  

        }else{


       $response['error'] = false; 
        $response['message'] = 'Invalid username or password';
    }

    echo json_encode($response);

【讨论】:

    【解决方案2】:

    欢迎您,@PeterSondak。你已经看过json documentation了吗?

    我想如果你用var_dump 测试你的$result 变量,你的结果会是这样的:

    object(stdClass)#1 (3) {
      ["error"]=>
      bool(false)
      ["message"]=>
      string(17) "Login successfull"
      ["user"]=>
      array(1) {
        [0]=>
        object(stdClass)#2 (7) {
          ["badgeid"]=>
          string(8) "10010080"
          ["email"]=>
          string(0) ""
          ["pwd"]=>
          string(0) ""
          ["fullname"]=>
          string(17) "AZWAN BIN SANIMIN"
          ["roles_id"]=>
          string(1) "7"
          ["team_id"]=>
          string(1) "2"
          ["users_id"]=>
          string(0) ""
        }
      }
    }
    

    如您所见,user 属性的值是一个数组。所以你应该把你的代码改成这样来获得第一个索引:

    $result = $json->otUserList[0]
    

    你会得到:

    "{"error":"false","message":"Login successfull","user":{"badgeid":"10010080","email":"","pwd":"","fullname":"AZWAN BIN SANIMIN","roles_id":"7","team_id":"2","users_id":""}}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多