douji8549 2017-02-14 03:11
浏览 12
已采纳

PHP解码JSON得到uname? [重复]

This question already has an answer here:

How do I get the uname value from this JSON into PHP variable? to use through my page?

    {
        "token": "iutiutiut-0jjjjj0-97987g",
        "auth": {
            "id": 1,
            "app_id": 1,
            "user": {
                "uname": "foo",  
                "role": "member"
            }
    }
}

thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.

could you also tell me the correct terminology so I know as well thanks

</div>
  • 写回答

1条回答 默认 最新

  • dua55014 2017-02-14 03:20
    关注

    What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:

    $obj = json_decode($json_string);
    $uname = $obj->auth->user->uname;
    

    Or, if you prefer the array syntax, use the second argument in json_decode:

    $arr = json_decode($json_string, true);
    $uname = $obj['auth']['user']['uname'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?