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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable