php - 如何在 php 中从此 JSON 获取元素

标签 php json orientdb

如何从 php 中的 json 中获取字段密码的值

[  
   {  
      "rid":"#145:0",
      "version":1,
      "oClass":"Login",
      "oData":{  
         "Password":"hacker007",
         "role":null,
         "Name":"Nijeesh Joshy",
         "Email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46282f2c2323352e72272a2a06212b272f2a6825292b" rel="noreferrer noopener nofollow">[email protected]</a>"
      }
   }
]

这是我的代码

$json = '[  
   {  
      "rid":"#145:0",
      "version":1,
      "oClass":"Login",
      "oData":{  
         "Password":"hacker007",
         "role":null,
         "Name":"Nijeesh Joshy",
         "Email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="197770737c7c6a712d787575597e74787075377a7674" rel="noreferrer noopener nofollow">[email protected]</a>"
      }
   }
]';

$json = json_decode($json,true);

echo $json[0]->oData->Name;

我收到此错误

Notice: Trying to get property of non-object

最佳答案

我发现在 json_decode() 步骤之后,属性不再受到保护:

$json_data = '[
    {
        "rid":"#145:0",
        "version":1,
        "oClass":"Login",
        "oData":{
            "Password":"hacker007",
            "role":null,
            "Name":"Nijeesh Joshy",
            "Email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="553b3c3f3030263d61343939153238343c397b363a38" rel="noreferrer noopener nofollow">[email protected]</a>"
        }
    }
]';

$data = json_decode($json_data);

因此,您可以通过以下方式访问数据:

$array['name'] = $data[0]->oData->Name;
$array['password'] = $data[0]->oData->Password;

var_dump($array);

输出:

array(2) { 
    ["password"]=> string(9) "hacker007" 
    ["name"]=> string(13) "Nijeesh Joshy" 
}

注意:

用于构建原始数据数组的类必须为您提供以正确方式获取数据的方法。

关于php - 如何在 php 中从此 JSON 获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357460/

相关文章:

php - 如何使用 CodeIgniter 在 MySQL 中更新数学集?

php - 从mysql中选择项目

json - 在 PostgreSQL 中创建约束时,有没有办法解决 JSON 数组的所有元素?

Firefox windows 7 中的 javascript 错误,在其他浏览器和操作系统中工作

php - WordPress 编辑管理员帮助选项卡

javascript - 如何将 JSON 转换为 GeoJSON

json - 用户权限更改后 JWT 刷新

java - 在没有 PropertyModel 的情况下将数据保存在 Wicket 中

date - 在orientDB中,如何搜索给定年份中的日期的条目?

node.js - 如果重点关注建模数据,那么哪个(内存中)图形数据库