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

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>

此问题已经存在 这里有一个答案: p>

  • 如何用PHP从JSON中提取数据? 5个答案\ r span> li> ul> div>

    如何从此JSON中获取uname值到PHP 变量? 通过我的页面使用? p>

      {
    “token”:“iutiutiut-0jjjjj0-97987g”,
    “auth”:{
    “id”:1,  
    “app_id”:1,
    “用户”:{
    “uname”:“foo”,
    “角色”:“成员”
    } 
    } 
    } 
      code>  
     
     

    感谢因为某些原因无法得到它而且我无法在谷歌的任何地方找到类似的例子,或者我没有正确地调用它。 p>

    你能不能告诉我正确的术语,所以我也知道谢谢 p> div>

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'];